{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "10f89794",
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "from numpy import * ;\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import scipy.linalg\n",
    "import os\n",
    "# 读取数据\n",
    "def load_csv(path):\n",
    "    data_read = pd.read_csv(path)\n",
    "    print(data_read.describe())\n",
    "    list = data_read.values.tolist()\n",
    "    data = np.array(list)\n",
    "    print(data.shape)\n",
    "    # print(data)\n",
    "    return data\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "434fe25a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       -0.432564812  -1.010633706  -1.187777016  -0.119869428  0.635274135  \\\n",
      "count     49.000000     49.000000     49.000000     49.000000    49.000000   \n",
      "mean       0.048962      0.078306     -0.215327     -0.017184    -0.043375   \n",
      "std        0.983680      0.747673      0.901642      0.991961     1.028094   \n",
      "min       -2.170674     -1.873990     -2.202321     -2.054325    -2.364590   \n",
      "25%       -0.691776     -0.350975     -0.989760     -0.627531    -0.644679   \n",
      "50%        0.125332      0.118445     -0.262440     -0.079330    -0.011787   \n",
      "75%        0.714325      0.591283      0.327368      0.535398     0.586939   \n",
      "max        2.183186      1.692430      1.488490      2.112160     2.309287   \n",
      "\n",
      "       0.990114872  -0.896042506  0.440909643  -0.071539489  -0.116988909  \\\n",
      "count    49.000000     49.000000    49.000000     49.000000     49.000000   \n",
      "mean     -0.149313     -0.034999     0.024138     -0.002479     -0.294872   \n",
      "std       0.889266      0.949705     1.062459      1.068394      0.859562   \n",
      "min      -1.664011     -2.449018    -2.008850     -2.379172     -2.520004   \n",
      "25%      -0.712085     -0.654708    -0.756209     -0.838188     -0.661443   \n",
      "50%      -0.133134     -0.035979    -0.198932      0.179841     -0.236144   \n",
      "75%       0.280880      0.536219     0.807650      0.671634      0.140072   \n",
      "max       2.136308      2.212554     2.372648      1.870453      1.537409   \n",
      "\n",
      "       ...  -0.063816177  -0.7225701  0.148397679  -0.935738409  -0.26601833  \\\n",
      "count  ...     49.000000   49.000000    49.000000     49.000000    49.000000   \n",
      "mean   ...      0.066979   -0.007545     0.047075      0.081991    -0.228918   \n",
      "std    ...      0.963553    1.031703     0.786508      0.967954     1.039850   \n",
      "min    ...     -2.728446   -3.115218    -1.768132     -2.024110    -2.202915   \n",
      "25%    ...     -0.239278   -0.788845    -0.496749     -0.644132    -0.938492   \n",
      "50%    ...      0.067073    0.096335     0.135621      0.343524    -0.328127   \n",
      "75%    ...      0.457088    0.680127     0.549549      0.833262     0.421070   \n",
      "max    ...      2.568181    1.914023     1.591907      1.853478     2.707325   \n",
      "\n",
      "       -0.329754655  1.699158967  0.524340066  -0.787931132  0.603589532  \n",
      "count     49.000000    49.000000    49.000000     49.000000    49.000000  \n",
      "mean      -0.221143     0.327517    -0.015934      0.136356    -0.055421  \n",
      "std        1.064510     1.070756     0.959609      1.005108     0.978258  \n",
      "min       -2.278697    -1.946768    -2.056192     -2.082894    -2.092094  \n",
      "25%       -1.025643    -0.347357    -0.629580     -0.386990    -0.788390  \n",
      "50%       -0.093345     0.119665     0.067099      0.075156    -0.066158  \n",
      "75%        0.504354     1.254021     0.697424      0.771678     0.512901  \n",
      "max        2.577836     2.117176     1.871478      2.570476     2.173058  \n",
      "\n",
      "[8 rows x 500 columns]\n",
      "(49, 500)\n",
      "       23.89339009\n",
      "count    49.000000\n",
      "mean      2.846758\n",
      "std      24.415053\n",
      "min     -52.734773\n",
      "25%     -12.044478\n",
      "50%       4.094061\n",
      "75%      22.220093\n",
      "max      65.948068\n",
      "(49, 1)\n",
      "(49, 501)\n",
      "(49, 1)\n",
      "(501, 1)\n"
     ]
    }
   ],
   "source": [
    "AFile = \"randn_data_regression_A.csv\"\n",
    "BFile = \"randn_data_regression_b.csv\"\n",
    "A = load_csv(AFile)\n",
    "B = load_csv(BFile)\n",
    "rows = A.shape[0]\n",
    "cols = A.shape[1]\n",
    "# 拓展矩阵第1列设置为1\n",
    "A=mat(np.c_[np.ones((rows,1)),A])\n",
    "rows = A.shape[0]\n",
    "cols = A.shape[1]\n",
    "B = mat(B)\n",
    "print(A.shape)\n",
    "print(B.shape)\n",
    "#创建theta矩阵\n",
    "theta = np.zeros((cols,1))\n",
    "print(theta.shape)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4225dc63",
   "metadata": {},
   "source": [
    "# 1)\t方法一：解线性方程组计算得到线性回归模型的参数\n",
    "\n",
    "#求解线性方程组求解方法\n",
    "#一 python自带函数求解 \n",
    "#报错 因为必须是 方阵, 也就是行数等于列数\n",
    "#theta = scipy.linalg.solve(A,B)\n",
    "#二 使用自己写的迭代法求解线性方程组的方法求解, 我是之前用matlab写的, 所以需要重新写 也必须是方阵?\n",
    "\n",
    "#AX = b  https://blog.csdn.net/skj1995/article/details/90114611 可逆的判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "cde2d7e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "矩阵不可逆,请采用梯度下降法或加正则项\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "before = A.T * A\n",
    "if (np.linalg.det(before)!=0):\n",
    "    theta = before.I*A.T*B \n",
    "    #θ=(x^-1X)^-1X^Ty\n",
    "    print(theta)\n",
    "else:\n",
    "    #可以看到A数据的组数比特征少\n",
    "    print(\"矩阵不可逆,请采用梯度下降法或加正则项\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75e90842",
   "metadata": {},
   "source": [
    "# 2)\t方法二：利用梯度下降法计算线性回归模型的参数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "ec78dc25",
   "metadata": {},
   "outputs": [],
   "source": [
    "def gradientDescent(X, y, theta, learning_rate, num_iters, tol,print_step):\n",
    "    #参数说明: X数据, y预测目标值, theta参数,learinig_rate 学习率  num_iters迭代次数 tol精度(0.01)\n",
    "    m = len(y)\n",
    "    J_history = np.zeros((num_iters, 1))\n",
    "\n",
    "    for i in range(num_iters):\n",
    "        n = len(theta)\n",
    "        theta_temp = theta\n",
    "#         print((X.dot(theta)-y).shape)\n",
    "#         print(X[:,1].shape)\n",
    "        for j in range(n):\n",
    "            theta_temp[j] = theta[j] - learning_rate/m*(X.dot(theta)-y).T.dot(X[:,j]); \n",
    "        theta = theta_temp \n",
    "        cost = computeCost(X, y, theta)\n",
    "        J_history[i] = cost\n",
    "        if(i%print_step==0):\n",
    "            print('第%d次迭代, cost = %f' %(i,cost))\n",
    "        if(cost < tol): #如果cost 偏差值 小于精确值 则退出\n",
    "            print('迭代训练结束,迭代次数:%d, 偏差值cost=%f'%(i,cost))\n",
    "            return (theta,J_history)\n",
    "    \n",
    "    return (theta,J_history)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "927fdf3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#计算偏差值\n",
    "def computeCost(X,y,theta):\n",
    "    # 这里均方误(欧式距离)差求偏差\n",
    "    m = len(y) #number of training examples\n",
    "    \n",
    "    inner = np.power(((X.dot(theta)) - y), 2) # (pred - y)^2\n",
    "    \n",
    "    return np.sum(inner) / (2 * m)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "86f6cc79",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第0次迭代, cost = 243.768989\n",
      "第10次迭代, cost = 44.050490\n",
      "第20次迭代, cost = 10.552179\n",
      "第30次迭代, cost = 2.902350\n",
      "第40次迭代, cost = 0.855970\n",
      "第50次迭代, cost = 0.262709\n",
      "第60次迭代, cost = 0.082717\n",
      "第70次迭代, cost = 0.026514\n",
      "第80次迭代, cost = 0.008612\n",
      "第90次迭代, cost = 0.002826\n",
      "第100次迭代, cost = 0.000935\n",
      "迭代训练结束,迭代次数:100, 偏差值cost=0.000935\n"
     ]
    }
   ],
   "source": [
    "# 这里是求解 方法二\n",
    "\n",
    "# 第一部分: 设定传入参数\n",
    "n = rows\n",
    "m = cols\n",
    "X0 = theta\n",
    "\n",
    "A = A\n",
    "B = B\n",
    "theta = np.zeros((m,1))\n",
    "learning_rate = 0.01\n",
    "step = 2000\n",
    "tol = 0.001\n",
    "print_step = 10\n",
    "\n",
    "#不可逆还可以使用加正则化项来做, 先写梯度下降方法吧\n",
    "\n",
    "# 第二部分: 梯度下降方法\n",
    "(theta,J_hist) = gradientDescent(A,B,theta,learning_rate,step,tol,print_step)\n",
    "#print(theta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "07c3abe6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.04634351e-01]\n",
      " [ 1.62400894e-02]\n",
      " [ 9.29961461e-02]\n",
      " [ 6.73010774e-02]\n",
      " [ 7.30270308e-01]\n",
      " [ 8.61464231e-01]\n",
      " [-3.06918121e-01]\n",
      " [-8.99312104e-03]\n",
      " [ 7.95697883e-01]\n",
      " [ 4.17147141e-01]\n",
      " [-6.30807306e-01]\n",
      " [-9.53032181e-02]\n",
      " [ 2.89267106e-01]\n",
      " [ 6.69853466e-01]\n",
      " [-2.12305784e-02]\n",
      " [ 4.93088308e-01]\n",
      " [ 1.35576311e-01]\n",
      " [-2.74558161e-01]\n",
      " [ 4.94648693e-01]\n",
      " [-4.59965800e-02]\n",
      " [ 6.08435859e-01]\n",
      " [ 4.86572210e-01]\n",
      " [ 8.08605003e-01]\n",
      " [ 7.12989582e-01]\n",
      " [-3.02549774e-01]\n",
      " [-8.31883374e-02]\n",
      " [ 1.55373770e-01]\n",
      " [ 7.16539711e-01]\n",
      " [ 3.44417789e-01]\n",
      " [ 7.08536863e-01]\n",
      " [ 4.95973160e-01]\n",
      " [ 3.82168021e-01]\n",
      " [-3.13244724e-01]\n",
      " [ 1.22889921e+00]\n",
      " [ 1.86272622e-01]\n",
      " [ 2.08047527e-02]\n",
      " [ 5.42738922e-01]\n",
      " [ 4.23724920e-01]\n",
      " [ 8.12797121e-01]\n",
      " [ 6.98345100e-01]\n",
      " [ 9.15665448e-02]\n",
      " [ 2.68957265e-02]\n",
      " [ 2.75499784e-01]\n",
      " [ 9.40905345e-01]\n",
      " [ 4.76747893e-02]\n",
      " [ 7.14117737e-01]\n",
      " [ 6.47808839e-01]\n",
      " [ 8.01784937e-02]\n",
      " [ 8.12837739e-01]\n",
      " [ 7.32994728e-01]\n",
      " [ 1.64845760e-01]\n",
      " [ 2.69518667e-01]\n",
      " [-8.53608197e-02]\n",
      " [-1.07182788e-01]\n",
      " [-3.49213213e-01]\n",
      " [ 4.87644116e-01]\n",
      " [ 1.34918280e-01]\n",
      " [-5.68150296e-01]\n",
      " [-1.08044802e-01]\n",
      " [ 3.13240742e-01]\n",
      " [-6.06036749e-03]\n",
      " [ 3.54098877e-02]\n",
      " [ 1.40778115e-02]\n",
      " [-1.03115533e+00]\n",
      " [-3.71938562e-02]\n",
      " [ 5.07033001e-01]\n",
      " [-3.61691296e-01]\n",
      " [ 4.52137418e-01]\n",
      " [-1.09474509e-01]\n",
      " [-3.08541842e-01]\n",
      " [ 2.19853977e-01]\n",
      " [-7.58446153e-02]\n",
      " [-3.71017815e-01]\n",
      " [ 6.43021844e-01]\n",
      " [-1.15989506e-01]\n",
      " [ 1.54799922e-01]\n",
      " [-2.63589798e-01]\n",
      " [-1.78214010e-02]\n",
      " [ 3.05706059e-03]\n",
      " [-1.08837950e+00]\n",
      " [ 1.41932627e-01]\n",
      " [-1.98569746e-01]\n",
      " [ 2.46818235e-01]\n",
      " [-9.83505269e-01]\n",
      " [ 1.81397238e-01]\n",
      " [-4.52125041e-02]\n",
      " [-8.79209660e-01]\n",
      " [ 8.45912591e-02]\n",
      " [ 3.94977827e-02]\n",
      " [-4.97150558e-01]\n",
      " [-3.29392418e-02]\n",
      " [ 6.55792752e-01]\n",
      " [ 1.16666127e-01]\n",
      " [-4.60025778e-01]\n",
      " [ 7.30466876e-01]\n",
      " [-1.38726548e-01]\n",
      " [ 1.81709528e-01]\n",
      " [ 5.98846017e-01]\n",
      " [-4.70808645e-01]\n",
      " [ 3.46655382e-01]\n",
      " [ 4.87464468e-01]\n",
      " [ 3.84166269e-01]\n",
      " [ 3.38805686e-01]\n",
      " [-4.63190658e-01]\n",
      " [-2.84550667e-01]\n",
      " [-7.03311625e-02]\n",
      " [-1.52102529e-01]\n",
      " [ 3.39532835e-01]\n",
      " [ 1.02754025e-01]\n",
      " [-7.78613451e-01]\n",
      " [-8.37842779e-02]\n",
      " [ 1.41339363e-01]\n",
      " [-4.58866036e-01]\n",
      " [ 7.71401576e-01]\n",
      " [-8.99127039e-02]\n",
      " [-1.05179673e-02]\n",
      " [-3.46055497e-01]\n",
      " [ 4.86428952e-01]\n",
      " [ 5.01717590e-01]\n",
      " [ 3.47079674e-01]\n",
      " [-1.27406111e-01]\n",
      " [-6.46208295e-01]\n",
      " [ 6.30526301e-02]\n",
      " [-1.54216062e-01]\n",
      " [-1.31120460e-01]\n",
      " [ 1.61862682e-01]\n",
      " [ 1.41963195e-01]\n",
      " [ 2.58364864e-02]\n",
      " [ 2.98178330e-01]\n",
      " [-1.14368888e-01]\n",
      " [-7.39423582e-02]\n",
      " [ 3.61372936e-01]\n",
      " [-6.21148031e-01]\n",
      " [-1.42896874e-01]\n",
      " [ 2.00293341e-01]\n",
      " [-9.50661295e-02]\n",
      " [-5.95268862e-01]\n",
      " [ 3.89628399e-01]\n",
      " [-2.49936477e-01]\n",
      " [-1.38372528e-01]\n",
      " [-2.00798585e-01]\n",
      " [ 3.93717369e-01]\n",
      " [-5.66187747e-01]\n",
      " [ 1.51228977e-01]\n",
      " [-2.26093356e-01]\n",
      " [ 8.69645984e-02]\n",
      " [ 5.18164306e-02]\n",
      " [-2.85554332e-01]\n",
      " [ 1.71045696e-03]\n",
      " [-4.70169312e-01]\n",
      " [ 2.78897175e-01]\n",
      " [ 1.29947913e-01]\n",
      " [ 2.69159761e-01]\n",
      " [-5.62661048e-01]\n",
      " [ 5.61631046e-02]\n",
      " [-3.27496153e-01]\n",
      " [-2.23248346e-01]\n",
      " [-1.50360076e-01]\n",
      " [ 2.00396953e-01]\n",
      " [-2.94654803e-01]\n",
      " [-2.88258630e-01]\n",
      " [ 6.56709634e-01]\n",
      " [ 2.34980613e-01]\n",
      " [-2.15025217e-01]\n",
      " [ 5.13363255e-01]\n",
      " [ 2.68059498e-01]\n",
      " [ 2.33117122e-01]\n",
      " [-3.86993473e-01]\n",
      " [ 3.76623045e-01]\n",
      " [ 1.80382346e-01]\n",
      " [ 1.78453935e-01]\n",
      " [-4.46977613e-01]\n",
      " [ 9.78862047e-03]\n",
      " [ 2.48191354e-01]\n",
      " [-8.49003054e-01]\n",
      " [-1.44621586e-01]\n",
      " [-4.21693622e-01]\n",
      " [-4.70446250e-01]\n",
      " [ 4.10842527e-01]\n",
      " [-1.22026214e-02]\n",
      " [-4.47805805e-01]\n",
      " [-2.23903638e-01]\n",
      " [ 3.93203272e-04]\n",
      " [-5.76842699e-02]\n",
      " [ 1.40902551e-01]\n",
      " [-2.48352369e-02]\n",
      " [-3.51501497e-01]\n",
      " [ 8.57273609e-02]\n",
      " [ 4.81231682e-01]\n",
      " [ 1.25456916e-01]\n",
      " [-4.56575476e-01]\n",
      " [-2.38410902e-01]\n",
      " [ 3.56740059e-01]\n",
      " [-8.59969446e-03]\n",
      " [-2.95734176e-01]\n",
      " [-1.65323345e-01]\n",
      " [ 4.44459591e-01]\n",
      " [ 1.00763587e-01]\n",
      " [ 1.56989328e-02]\n",
      " [ 4.80448284e-02]\n",
      " [-4.94013714e-03]\n",
      " [-2.09641960e-01]\n",
      " [ 1.85481925e-01]\n",
      " [ 1.61814820e-01]\n",
      " [ 3.79911619e-01]\n",
      " [ 3.57010530e-01]\n",
      " [-5.19667884e-01]\n",
      " [-1.87864230e-01]\n",
      " [-4.13214494e-01]\n",
      " [-2.36948057e-01]\n",
      " [-1.69142275e-01]\n",
      " [ 5.39500868e-02]\n",
      " [ 3.48567626e-01]\n",
      " [-1.07949070e-01]\n",
      " [-3.40952543e-01]\n",
      " [-9.82199963e-02]\n",
      " [ 4.27958398e-01]\n",
      " [ 4.83080543e-01]\n",
      " [-2.93267097e-01]\n",
      " [ 8.75282875e-01]\n",
      " [-1.05550422e-01]\n",
      " [-3.60789500e-01]\n",
      " [-1.60879504e-01]\n",
      " [-3.49645778e-01]\n",
      " [-4.35868728e-02]\n",
      " [-4.64158833e-03]\n",
      " [ 7.01151764e-02]\n",
      " [-2.72001179e-01]\n",
      " [-2.81776033e-02]\n",
      " [-5.67123293e-02]\n",
      " [ 1.84931864e-01]\n",
      " [ 2.14177438e-01]\n",
      " [-4.79852076e-01]\n",
      " [ 3.83869030e-01]\n",
      " [ 3.72907671e-02]\n",
      " [-4.10472667e-01]\n",
      " [-3.19848127e-01]\n",
      " [ 5.40709905e-01]\n",
      " [-4.11642274e-01]\n",
      " [-7.07310524e-01]\n",
      " [ 7.11357158e-02]\n",
      " [ 1.36667241e-01]\n",
      " [-5.32366850e-01]\n",
      " [-1.58119689e-01]\n",
      " [-5.92383538e-01]\n",
      " [-5.33428567e-01]\n",
      " [-6.69626197e-02]\n",
      " [ 2.48119068e-01]\n",
      " [-8.91014598e-02]\n",
      " [ 8.61884233e-01]\n",
      " [ 2.65713548e-01]\n",
      " [-7.20582109e-02]\n",
      " [ 5.67255836e-01]\n",
      " [ 4.58915257e-01]\n",
      " [ 4.55887660e-01]\n",
      " [-4.24023606e-02]\n",
      " [ 4.63571502e-01]\n",
      " [-4.86654024e-03]\n",
      " [ 1.52172010e-01]\n",
      " [-4.94660490e-01]\n",
      " [-8.24645047e-02]\n",
      " [ 6.05159995e-01]\n",
      " [-6.79423577e-02]\n",
      " [-1.37227468e-01]\n",
      " [ 6.15755187e-02]\n",
      " [ 1.00688858e-01]\n",
      " [ 1.56124070e-01]\n",
      " [ 6.12990114e-01]\n",
      " [ 1.17317078e-01]\n",
      " [-1.32125789e-01]\n",
      " [-8.63741937e-01]\n",
      " [-2.50432839e-01]\n",
      " [ 1.58252347e-01]\n",
      " [ 3.00026970e-01]\n",
      " [ 1.37724694e-01]\n",
      " [ 3.21043321e-01]\n",
      " [-1.45050655e-01]\n",
      " [ 1.53376844e-01]\n",
      " [-1.95093142e-02]\n",
      " [ 1.47057294e-01]\n",
      " [ 3.89336020e-01]\n",
      " [-3.80596470e-01]\n",
      " [-3.47889282e-01]\n",
      " [ 9.60549316e-02]\n",
      " [ 3.25676888e-01]\n",
      " [-1.17761687e-01]\n",
      " [ 9.22634678e-03]\n",
      " [ 3.15820245e-01]\n",
      " [ 3.26091723e-01]\n",
      " [-2.78782121e-01]\n",
      " [ 2.16705425e-01]\n",
      " [-3.88775543e-02]\n",
      " [-5.18925844e-01]\n",
      " [-2.09970441e-01]\n",
      " [ 1.65209647e-01]\n",
      " [ 4.81343820e-01]\n",
      " [-9.78655147e-02]\n",
      " [ 2.79966800e-01]\n",
      " [-6.91278273e-02]\n",
      " [-6.79599875e-01]\n",
      " [ 2.19712906e-02]\n",
      " [-2.20309125e-01]\n",
      " [ 7.59916397e-02]\n",
      " [ 2.64690862e-01]\n",
      " [-1.62676080e-01]\n",
      " [ 1.07948368e-01]\n",
      " [ 3.04245316e-01]\n",
      " [-1.59874338e-01]\n",
      " [-2.53055131e-01]\n",
      " [ 4.81912151e-01]\n",
      " [ 1.38411680e-02]\n",
      " [-8.73315304e-02]\n",
      " [-1.10099712e-01]\n",
      " [ 1.46187339e-02]\n",
      " [-9.33948482e-01]\n",
      " [ 1.62276081e-01]\n",
      " [ 7.03839263e-01]\n",
      " [ 5.07612220e-02]\n",
      " [ 2.21581733e-04]\n",
      " [-3.14128059e-01]\n",
      " [-4.02787871e-01]\n",
      " [-4.03503012e-01]\n",
      " [ 6.92546529e-01]\n",
      " [-3.33853016e-01]\n",
      " [-3.39405501e-01]\n",
      " [-1.11920729e-01]\n",
      " [ 1.20935526e-01]\n",
      " [-4.07381089e-01]\n",
      " [ 1.58759705e-01]\n",
      " [ 1.11627361e-01]\n",
      " [ 4.84527082e-02]\n",
      " [-5.99430751e-01]\n",
      " [ 6.58261493e-02]\n",
      " [ 2.32370849e-01]\n",
      " [ 6.04710292e-01]\n",
      " [ 3.32015499e-01]\n",
      " [ 7.74689077e-02]\n",
      " [ 2.45779879e-02]\n",
      " [-9.93123577e-02]\n",
      " [ 3.77298162e-01]\n",
      " [-4.22139666e-01]\n",
      " [-1.46088383e-01]\n",
      " [-8.53746199e-02]\n",
      " [-1.20177929e-01]\n",
      " [-2.03353933e-02]\n",
      " [ 5.85211918e-02]\n",
      " [ 9.01918469e-02]\n",
      " [-1.96194534e-01]\n",
      " [ 3.57229544e-02]\n",
      " [-8.81137545e-02]\n",
      " [-1.00207523e-01]\n",
      " [-3.27064084e-01]\n",
      " [ 5.27978984e-01]\n",
      " [-3.06398177e-01]\n",
      " [-1.65855902e-02]\n",
      " [ 2.09297486e-01]\n",
      " [-7.35584618e-01]\n",
      " [-1.79172612e-01]\n",
      " [-2.08438846e-01]\n",
      " [-3.13339027e-01]\n",
      " [-5.41771760e-01]\n",
      " [-1.81078618e-01]\n",
      " [ 3.86651188e-01]\n",
      " [ 2.11027384e-01]\n",
      " [-1.23559967e-01]\n",
      " [ 9.07672812e-02]\n",
      " [ 1.61731745e-01]\n",
      " [ 6.38602458e-02]\n",
      " [ 3.46579206e-01]\n",
      " [-1.87152410e-01]\n",
      " [-5.34704223e-01]\n",
      " [-2.13315897e-01]\n",
      " [-1.15059362e-01]\n",
      " [-4.70144709e-01]\n",
      " [ 3.25792586e-01]\n",
      " [ 4.77112036e-01]\n",
      " [ 1.97197153e-02]\n",
      " [-5.85357456e-01]\n",
      " [-5.01176270e-02]\n",
      " [-9.92994988e-02]\n",
      " [-1.66841267e-01]\n",
      " [ 1.48304608e-01]\n",
      " [-3.37086338e-01]\n",
      " [-2.18122430e-01]\n",
      " [-3.16772750e-01]\n",
      " [ 2.42488204e-01]\n",
      " [ 3.32966650e-01]\n",
      " [-1.20469917e-01]\n",
      " [ 3.43699760e-01]\n",
      " [ 2.60526237e-01]\n",
      " [ 9.08775573e-02]\n",
      " [ 2.80555737e-01]\n",
      " [ 1.25800785e-01]\n",
      " [-5.63380843e-01]\n",
      " [ 1.59821943e-01]\n",
      " [ 2.27929176e-01]\n",
      " [-1.54897834e-01]\n",
      " [-2.95155150e-01]\n",
      " [ 8.29662783e-01]\n",
      " [ 4.85891992e-01]\n",
      " [-5.27356794e-01]\n",
      " [-5.54111701e-01]\n",
      " [-1.07039153e-01]\n",
      " [ 1.66998664e-01]\n",
      " [-1.62212017e-01]\n",
      " [ 4.03045313e-01]\n",
      " [-4.08415527e-01]\n",
      " [ 5.41539301e-01]\n",
      " [ 9.51456716e-02]\n",
      " [-2.62308687e-01]\n",
      " [ 2.44284365e-01]\n",
      " [-5.03805075e-02]\n",
      " [-3.36743987e-01]\n",
      " [ 1.03318025e-01]\n",
      " [ 6.79794468e-01]\n",
      " [-2.14762992e-01]\n",
      " [-8.12574255e-01]\n",
      " [-3.65703371e-01]\n",
      " [-1.80948059e-01]\n",
      " [ 2.79648101e-01]\n",
      " [ 4.09179357e-01]\n",
      " [ 4.34059267e-01]\n",
      " [ 1.36783333e-01]\n",
      " [-1.04107518e-01]\n",
      " [-4.59066254e-02]\n",
      " [-3.82013897e-01]\n",
      " [-4.25405714e-01]\n",
      " [ 8.75831573e-01]\n",
      " [ 5.58167434e-01]\n",
      " [-7.98998428e-01]\n",
      " [-3.19876195e-01]\n",
      " [-1.45036502e-01]\n",
      " [ 9.01718819e-01]\n",
      " [ 5.62771498e-01]\n",
      " [ 2.96348344e-01]\n",
      " [-3.37642992e-01]\n",
      " [ 4.35558914e-01]\n",
      " [-1.94357105e-01]\n",
      " [-1.10936285e-01]\n",
      " [ 2.65957413e-01]\n",
      " [ 7.43377473e-01]\n",
      " [-2.15387791e-01]\n",
      " [ 4.88384519e-01]\n",
      " [ 9.19600695e-03]\n",
      " [-1.17364971e-01]\n",
      " [-1.12805874e-01]\n",
      " [ 2.67341879e-01]\n",
      " [-3.43512600e-01]\n",
      " [ 7.66492010e-02]\n",
      " [ 5.18699992e-02]\n",
      " [ 4.00226563e-02]\n",
      " [ 2.14835296e-02]\n",
      " [-6.11909797e-01]\n",
      " [ 1.77803460e-01]\n",
      " [ 1.11291400e-01]\n",
      " [ 2.17191869e-01]\n",
      " [ 4.58442715e-01]\n",
      " [ 2.72875604e-01]\n",
      " [-7.75204485e-01]\n",
      " [-4.69583987e-01]\n",
      " [-4.51663525e-01]\n",
      " [-2.04753355e-01]\n",
      " [ 1.08976100e-02]\n",
      " [-1.17432544e-01]\n",
      " [ 1.87616499e-01]\n",
      " [-2.99357712e-01]\n",
      " [-4.89245535e-01]\n",
      " [-2.37978945e-01]\n",
      " [-5.69447177e-02]\n",
      " [ 3.77972793e-01]\n",
      " [-2.94777796e-01]\n",
      " [ 8.23967397e-01]\n",
      " [-2.53809683e-01]\n",
      " [-1.29053682e-01]\n",
      " [ 1.60943116e-01]\n",
      " [ 4.51664064e-01]\n",
      " [-3.03808193e-01]\n",
      " [-1.90090202e-01]\n",
      " [ 4.02811254e-01]\n",
      " [ 3.01354835e-01]\n",
      " [ 1.38880453e-01]\n",
      " [-4.16487672e-01]\n",
      " [-6.48195288e-01]\n",
      " [ 2.37887242e-01]\n",
      " [-2.71690847e-01]\n",
      " [-2.48790952e-01]\n",
      " [-2.07387230e-01]\n",
      " [-3.01136433e-01]\n",
      " [-1.97002316e-01]\n",
      " [-2.62106607e-01]\n",
      " [ 2.37732766e-01]\n",
      " [-2.88200618e-01]\n",
      " [-1.59497366e-02]\n",
      " [-2.39807090e-01]\n",
      " [ 2.46935381e-01]\n",
      " [ 2.12575078e-01]\n",
      " [-3.13498123e-01]\n",
      " [ 1.98397440e-01]\n",
      " [ 1.31526891e-01]\n",
      " [-8.33835140e-02]\n",
      " [ 6.14633186e-01]]\n"
     ]
    }
   ],
   "source": [
    "print(theta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "82d48608",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1a374acb288>]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAaE0lEQVR4nO3dfXRc9X3n8fd3NJLGen60LT8b40DsGIhRCAnZrLdAeCiNk2zaY5om7Da77jZkN+lyTgrJnm2357BLt3lo2Ka0JNDQLps0C0lxKRCIS5eQpAaZAH7CsYKfkW35UbaFZEnz3T/uFR7LEh49jO7MvZ/XOXNm5nfvjL6+9nz08+/+5nfN3RERkXhJRV2AiIhMPYW7iEgMKdxFRGJI4S4iEkMKdxGRGEpHXQBAS0uLL1q0KOoyRERKysaNGw+7e+to24oi3BctWkRHR0fUZYiIlBQz2z3WtgsOy5jZfDN71sy2mtkWM/tc2P6HZrbfzF4ObzfnvOYuM+s0s+1mdsPU/DFERCRf+fTcB4E73P0lM6sFNprZM+G2r7n7l3N3NrNlwBpgOTAH+JGZvcPdh6aycBERGdsFe+7u3uXuL4WPTwLbgLlv85LVwHfdvd/ddwKdwFVTUayIiORnXLNlzGwR8G5gQ9j0WTN71cweNLPGsG0usDfnZfsY5ZeBma01sw4z6+ju7h5/5SIiMqa8w93MaoBHgc+7ew9wH7AEuALoAr4ynh/s7ve7e7u7t7e2jnqyV0REJiivcDezcoJgf9jdvw/g7gfdfcjds8A3OTv0sh+Yn/PyeWGbiIhMk3xmyxjw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3ebB7/iI5t/ama3AIfcfWNUNeQhDawE7nP3dwOnGTEEE+VxDMetVxP8EpoDVDPKf+WLTdT/9i7EzL5EMLT5cNS1DDOzKuCLwH+NupbxKuVwL8qLcJtZOUGwP+zu3w+bD5pZW7i9DTgUVX3ANcCHzWwX8F2CoZmvAw1mNnxlrqiP5T5gn7tvCJ8/QhD2xXIcrwN2unu3uw8A3yc4rsV0DIeNdcyK6vNjZv8GuAX4RPhLCIqjxiUEv8RfCT8z84CXzGx2kdQ3plIO96K7CLeZGfAAsM3dv5qzaR1wW/j4NoKx+Ei4+13uPs/dFxEcs390908AzwIfD3eLusYDwF4zuyRsuhbYSvEcxz3A1WZWFf6dD9dXNMcwx1jHbB3wqXDGx9XAiZzhm2llZjcSDBN+2N17czatA9aYWaWZLSY4cfnCdNbm7pvcfaa7Lwo/M/uAleG/0aI5hqOKetB/kic+biY4u/5L4EtFUM8HCP7b+yrwcni7mWBMez2wA/gR0BR1rWG9q4DHw8cXEXxwOoH/C1RGXNsVQEd4LP8OaCym4wj8N+A1YDPwN0Bl1McQ+A7BOYABghD69FjHjOAk+jfCz84mgpk/UdXYSTB2PfyZ+Yuc/b8U1rgduCmK+kZs38XZE6qRHMN8b1p+QEQkhkp5WEZERMagcBcRiSGFu4hIDCncRURiSOEuIhJDCncRkRhSuIuIxND/BxPmlngxgHCmAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(J_hist.size)\n",
    "\n",
    "x_axis = np.linspace(0,150,150)\n",
    "plt.plot(x_axis,J_hist[0:150])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c093fefb",
   "metadata": {},
   "source": [
    "# 3)\t在线性回归模型中加入正则化项 ，实现计算过程。 \n",
    "\n",
    "#正则化之后 对于 梯度下降法和 normalequation 正规方程方法算法都有所改变\n",
    "\n",
    "#好处是对于梯度下降 可以避免 过拟合\n",
    "\n",
    "#对于normalequation 可以直接求出,不可逆的问题消失了 数学上的证明 比较高深..\n",
    "\n",
    "#下面写求解\n",
    "#先给出正规方程的求解\n",
    "#再修改函数适应正则化\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72437dd5",
   "metadata": {},
   "source": [
    "## 正规方程求解\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "86ad850f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "251001\n",
      "嘿嘿,加上就可逆了,瞬间求出,看看数量级基本相同参数位置和梯度下降方法求解的是一致的\n",
      "[[ 1.91105718e-01]\n",
      " [ 1.38228138e-02]\n",
      " [ 8.87805096e-02]\n",
      " [ 5.15552592e-02]\n",
      " [ 6.97645377e-01]\n",
      " [ 8.22321331e-01]\n",
      " [-2.97682812e-01]\n",
      " [-6.99771743e-03]\n",
      " [ 7.60504091e-01]\n",
      " [ 4.02088502e-01]\n",
      " [-6.05558181e-01]\n",
      " [-9.85914265e-02]\n",
      " [ 2.85109995e-01]\n",
      " [ 6.50452153e-01]\n",
      " [-1.73537776e-02]\n",
      " [ 4.74840468e-01]\n",
      " [ 1.26846271e-01]\n",
      " [-2.57215652e-01]\n",
      " [ 4.71495965e-01]\n",
      " [-3.53374343e-02]\n",
      " [ 5.87773603e-01]\n",
      " [ 4.84782297e-01]\n",
      " [ 7.77023078e-01]\n",
      " [ 6.94140275e-01]\n",
      " [-2.85080636e-01]\n",
      " [-8.50840457e-02]\n",
      " [ 1.61033772e-01]\n",
      " [ 6.93865542e-01]\n",
      " [ 3.34486511e-01]\n",
      " [ 6.89247830e-01]\n",
      " [ 4.73454395e-01]\n",
      " [ 3.63112352e-01]\n",
      " [-3.10833987e-01]\n",
      " [ 1.18308590e+00]\n",
      " [ 1.73653361e-01]\n",
      " [ 2.27667118e-02]\n",
      " [ 5.20458415e-01]\n",
      " [ 4.08106821e-01]\n",
      " [ 7.98166795e-01]\n",
      " [ 6.78951774e-01]\n",
      " [ 8.47887361e-02]\n",
      " [ 1.13951547e-02]\n",
      " [ 2.65804009e-01]\n",
      " [ 9.08392092e-01]\n",
      " [ 5.28263152e-02]\n",
      " [ 6.93253265e-01]\n",
      " [ 6.23598427e-01]\n",
      " [ 7.17934353e-02]\n",
      " [ 7.87432851e-01]\n",
      " [ 7.11476125e-01]\n",
      " [ 1.61870755e-01]\n",
      " [ 2.51967107e-01]\n",
      " [-8.08543864e-02]\n",
      " [-1.05617440e-01]\n",
      " [-3.38617102e-01]\n",
      " [ 4.67371035e-01]\n",
      " [ 1.31885635e-01]\n",
      " [-5.52538474e-01]\n",
      " [-1.06034182e-01]\n",
      " [ 3.01877757e-01]\n",
      " [-8.71207986e-03]\n",
      " [ 3.92523985e-02]\n",
      " [ 8.16391455e-03]\n",
      " [-1.00600777e+00]\n",
      " [-3.66817691e-02]\n",
      " [ 4.97429636e-01]\n",
      " [-3.51487309e-01]\n",
      " [ 4.37750211e-01]\n",
      " [-1.09065601e-01]\n",
      " [-2.98983658e-01]\n",
      " [ 2.17011878e-01]\n",
      " [-7.21608079e-02]\n",
      " [-3.60837596e-01]\n",
      " [ 6.29386271e-01]\n",
      " [-1.14917997e-01]\n",
      " [ 1.56041179e-01]\n",
      " [-2.59085350e-01]\n",
      " [-2.01207464e-02]\n",
      " [ 1.23736246e-03]\n",
      " [-1.06596104e+00]\n",
      " [ 1.37253447e-01]\n",
      " [-1.89454616e-01]\n",
      " [ 2.38541702e-01]\n",
      " [-9.61020772e-01]\n",
      " [ 1.75956660e-01]\n",
      " [-4.36569345e-02]\n",
      " [-8.65146448e-01]\n",
      " [ 8.41050321e-02]\n",
      " [ 3.62503950e-02]\n",
      " [-5.01095073e-01]\n",
      " [-3.84154537e-02]\n",
      " [ 6.42581906e-01]\n",
      " [ 1.13533792e-01]\n",
      " [-4.49545028e-01]\n",
      " [ 7.21213819e-01]\n",
      " [-1.40235254e-01]\n",
      " [ 1.72450929e-01]\n",
      " [ 5.90769653e-01]\n",
      " [-4.57267533e-01]\n",
      " [ 3.38424984e-01]\n",
      " [ 4.88914937e-01]\n",
      " [ 3.72870993e-01]\n",
      " [ 3.28621850e-01]\n",
      " [-4.57704722e-01]\n",
      " [-2.79456414e-01]\n",
      " [-6.71666123e-02]\n",
      " [-1.56367277e-01]\n",
      " [ 3.25992482e-01]\n",
      " [ 1.03013716e-01]\n",
      " [-7.62623733e-01]\n",
      " [-8.08361923e-02]\n",
      " [ 1.41029313e-01]\n",
      " [-4.44129165e-01]\n",
      " [ 7.60589642e-01]\n",
      " [-9.06448758e-02]\n",
      " [-1.04116790e-02]\n",
      " [-3.40091904e-01]\n",
      " [ 4.79996017e-01]\n",
      " [ 4.94064602e-01]\n",
      " [ 3.40316902e-01]\n",
      " [-1.24230752e-01]\n",
      " [-6.39053175e-01]\n",
      " [ 5.93271377e-02]\n",
      " [-1.51366695e-01]\n",
      " [-1.29043273e-01]\n",
      " [ 1.60301957e-01]\n",
      " [ 1.40931194e-01]\n",
      " [ 2.65897897e-02]\n",
      " [ 2.95767270e-01]\n",
      " [-1.09192112e-01]\n",
      " [-6.67024106e-02]\n",
      " [ 3.53290395e-01]\n",
      " [-6.14004510e-01]\n",
      " [-1.43301104e-01]\n",
      " [ 2.02477754e-01]\n",
      " [-1.03885525e-01]\n",
      " [-5.95133851e-01]\n",
      " [ 3.87029548e-01]\n",
      " [-2.48083298e-01]\n",
      " [-1.31583574e-01]\n",
      " [-1.96942123e-01]\n",
      " [ 3.95839556e-01]\n",
      " [-5.64311174e-01]\n",
      " [ 1.46333676e-01]\n",
      " [-2.18468118e-01]\n",
      " [ 8.24542491e-02]\n",
      " [ 4.50594426e-02]\n",
      " [-2.78177377e-01]\n",
      " [ 6.29909943e-04]\n",
      " [-4.67173639e-01]\n",
      " [ 2.75468849e-01]\n",
      " [ 1.23055238e-01]\n",
      " [ 2.70266656e-01]\n",
      " [-5.52916827e-01]\n",
      " [ 5.06009525e-02]\n",
      " [-3.19578814e-01]\n",
      " [-2.26263424e-01]\n",
      " [-1.46350768e-01]\n",
      " [ 2.04176797e-01]\n",
      " [-2.88405765e-01]\n",
      " [-2.84085394e-01]\n",
      " [ 6.46263235e-01]\n",
      " [ 2.34725135e-01]\n",
      " [-2.18237718e-01]\n",
      " [ 5.18772891e-01]\n",
      " [ 2.63566353e-01]\n",
      " [ 2.25518805e-01]\n",
      " [-3.90014192e-01]\n",
      " [ 3.74986234e-01]\n",
      " [ 1.77912057e-01]\n",
      " [ 1.79616794e-01]\n",
      " [-4.52002575e-01]\n",
      " [ 8.19958112e-03]\n",
      " [ 2.43412086e-01]\n",
      " [-8.46863940e-01]\n",
      " [-1.43978219e-01]\n",
      " [-4.21076721e-01]\n",
      " [-4.66863356e-01]\n",
      " [ 4.12316238e-01]\n",
      " [-1.01112023e-02]\n",
      " [-4.44969616e-01]\n",
      " [-2.25439850e-01]\n",
      " [ 1.28915465e-02]\n",
      " [-6.06233770e-02]\n",
      " [ 1.39718170e-01]\n",
      " [-2.51207940e-02]\n",
      " [-3.52377530e-01]\n",
      " [ 9.10931831e-02]\n",
      " [ 4.80770825e-01]\n",
      " [ 1.23042050e-01]\n",
      " [-4.60807636e-01]\n",
      " [-2.39732553e-01]\n",
      " [ 3.59100262e-01]\n",
      " [-7.78305035e-03]\n",
      " [-2.91279675e-01]\n",
      " [-1.74310159e-01]\n",
      " [ 4.43635620e-01]\n",
      " [ 1.08866993e-01]\n",
      " [ 1.43782973e-02]\n",
      " [ 4.49680350e-02]\n",
      " [ 3.83258370e-04]\n",
      " [-2.10094142e-01]\n",
      " [ 1.83037113e-01]\n",
      " [ 1.65712603e-01]\n",
      " [ 3.80590733e-01]\n",
      " [ 3.52053704e-01]\n",
      " [-5.22234439e-01]\n",
      " [-1.78782679e-01]\n",
      " [-4.09845936e-01]\n",
      " [-2.43136980e-01]\n",
      " [-1.77539069e-01]\n",
      " [ 5.49825105e-02]\n",
      " [ 3.49105173e-01]\n",
      " [-1.11999534e-01]\n",
      " [-3.41825862e-01]\n",
      " [-1.07242691e-01]\n",
      " [ 4.23705595e-01]\n",
      " [ 4.82936541e-01]\n",
      " [-2.91793852e-01]\n",
      " [ 8.81117830e-01]\n",
      " [-1.13118436e-01]\n",
      " [-3.66354670e-01]\n",
      " [-1.53025562e-01]\n",
      " [-3.48585528e-01]\n",
      " [-3.65537228e-02]\n",
      " [-1.66866214e-03]\n",
      " [ 7.39627817e-02]\n",
      " [-2.77622161e-01]\n",
      " [-3.40238486e-02]\n",
      " [-5.17829961e-02]\n",
      " [ 1.84642319e-01]\n",
      " [ 2.15608301e-01]\n",
      " [-4.77190578e-01]\n",
      " [ 3.84088631e-01]\n",
      " [ 4.85071741e-02]\n",
      " [-4.12661725e-01]\n",
      " [-3.12982033e-01]\n",
      " [ 5.45443261e-01]\n",
      " [-4.07760740e-01]\n",
      " [-7.09994019e-01]\n",
      " [ 6.66205311e-02]\n",
      " [ 1.35444072e-01]\n",
      " [-5.39983686e-01]\n",
      " [-1.59614807e-01]\n",
      " [-5.96220849e-01]\n",
      " [-5.30296724e-01]\n",
      " [-6.24766910e-02]\n",
      " [ 2.58340927e-01]\n",
      " [-9.29748775e-02]\n",
      " [ 8.70624341e-01]\n",
      " [ 2.65118795e-01]\n",
      " [-7.21107182e-02]\n",
      " [ 5.68964986e-01]\n",
      " [ 4.54701242e-01]\n",
      " [ 4.60535995e-01]\n",
      " [-4.02299397e-02]\n",
      " [ 4.65479009e-01]\n",
      " [-1.71811778e-03]\n",
      " [ 1.48767381e-01]\n",
      " [-5.03700462e-01]\n",
      " [-7.61163512e-02]\n",
      " [ 6.13424109e-01]\n",
      " [-6.71590037e-02]\n",
      " [-1.39178533e-01]\n",
      " [ 6.77203938e-02]\n",
      " [ 1.06740599e-01]\n",
      " [ 1.57221243e-01]\n",
      " [ 6.20671589e-01]\n",
      " [ 1.17931889e-01]\n",
      " [-1.31401334e-01]\n",
      " [-8.68249925e-01]\n",
      " [-2.50273440e-01]\n",
      " [ 1.52214128e-01]\n",
      " [ 3.04410747e-01]\n",
      " [ 1.22811235e-01]\n",
      " [ 3.25843628e-01]\n",
      " [-1.53251898e-01]\n",
      " [ 1.60637346e-01]\n",
      " [-1.34613880e-02]\n",
      " [ 1.49107215e-01]\n",
      " [ 3.86612570e-01]\n",
      " [-3.89147646e-01]\n",
      " [-3.40651159e-01]\n",
      " [ 9.97712792e-02]\n",
      " [ 3.32005177e-01]\n",
      " [-1.22569442e-01]\n",
      " [ 9.48743899e-03]\n",
      " [ 3.17661675e-01]\n",
      " [ 3.30151814e-01]\n",
      " [-2.82343982e-01]\n",
      " [ 2.40020386e-01]\n",
      " [-3.95614988e-02]\n",
      " [-5.24332156e-01]\n",
      " [-2.08172599e-01]\n",
      " [ 1.64913867e-01]\n",
      " [ 4.89392935e-01]\n",
      " [-1.05555090e-01]\n",
      " [ 2.89849934e-01]\n",
      " [-6.94481580e-02]\n",
      " [-6.83003015e-01]\n",
      " [ 2.24643503e-02]\n",
      " [-2.24945989e-01]\n",
      " [ 7.87002925e-02]\n",
      " [ 2.77085629e-01]\n",
      " [-1.65145181e-01]\n",
      " [ 1.07000987e-01]\n",
      " [ 3.14472442e-01]\n",
      " [-1.67207488e-01]\n",
      " [-2.65833299e-01]\n",
      " [ 4.79599457e-01]\n",
      " [ 1.28070768e-02]\n",
      " [-9.59043291e-02]\n",
      " [-1.05055033e-01]\n",
      " [ 1.91920930e-02]\n",
      " [-9.39580793e-01]\n",
      " [ 1.65230850e-01]\n",
      " [ 7.08394316e-01]\n",
      " [ 5.27025181e-02]\n",
      " [ 4.21886271e-03]\n",
      " [-3.13445821e-01]\n",
      " [-4.06677353e-01]\n",
      " [-4.08895303e-01]\n",
      " [ 7.02091922e-01]\n",
      " [-3.39022980e-01]\n",
      " [-3.46227178e-01]\n",
      " [-1.07188679e-01]\n",
      " [ 1.19345702e-01]\n",
      " [-4.09884565e-01]\n",
      " [ 1.51337131e-01]\n",
      " [ 1.09288645e-01]\n",
      " [ 5.58594560e-02]\n",
      " [-6.08703126e-01]\n",
      " [ 6.83634661e-02]\n",
      " [ 2.32338372e-01]\n",
      " [ 6.16113586e-01]\n",
      " [ 3.44304552e-01]\n",
      " [ 7.64273125e-02]\n",
      " [ 2.63390898e-02]\n",
      " [-1.01239850e-01]\n",
      " [ 3.74436563e-01]\n",
      " [-4.33131414e-01]\n",
      " [-1.45950772e-01]\n",
      " [-7.98485100e-02]\n",
      " [-1.26506685e-01]\n",
      " [-2.05717995e-02]\n",
      " [ 7.30090469e-02]\n",
      " [ 8.39438611e-02]\n",
      " [-2.01789362e-01]\n",
      " [ 3.55222733e-02]\n",
      " [-9.40303596e-02]\n",
      " [-1.01544094e-01]\n",
      " [-3.27241848e-01]\n",
      " [ 5.40196567e-01]\n",
      " [-3.04978389e-01]\n",
      " [-2.04711369e-02]\n",
      " [ 2.11770677e-01]\n",
      " [-7.40413015e-01]\n",
      " [-1.74822775e-01]\n",
      " [-2.13018427e-01]\n",
      " [-3.19438785e-01]\n",
      " [-5.45242300e-01]\n",
      " [-1.76275498e-01]\n",
      " [ 3.88925775e-01]\n",
      " [ 2.14307624e-01]\n",
      " [-1.25940555e-01]\n",
      " [ 8.57246918e-02]\n",
      " [ 1.55051286e-01]\n",
      " [ 6.76669289e-02]\n",
      " [ 3.46329037e-01]\n",
      " [-1.89947306e-01]\n",
      " [-5.46831498e-01]\n",
      " [-2.17999765e-01]\n",
      " [-1.18111633e-01]\n",
      " [-4.73632439e-01]\n",
      " [ 3.30260272e-01]\n",
      " [ 4.86061077e-01]\n",
      " [ 1.37323256e-02]\n",
      " [-5.92477259e-01]\n",
      " [-4.90874287e-02]\n",
      " [-9.87169799e-02]\n",
      " [-1.73632363e-01]\n",
      " [ 1.43881042e-01]\n",
      " [-3.47060017e-01]\n",
      " [-2.15484679e-01]\n",
      " [-3.23707818e-01]\n",
      " [ 2.54693941e-01]\n",
      " [ 3.44808427e-01]\n",
      " [-1.18639191e-01]\n",
      " [ 3.54031200e-01]\n",
      " [ 2.72423791e-01]\n",
      " [ 9.18611581e-02]\n",
      " [ 2.89526713e-01]\n",
      " [ 1.29122393e-01]\n",
      " [-5.63497819e-01]\n",
      " [ 1.63659307e-01]\n",
      " [ 2.32019302e-01]\n",
      " [-1.61130008e-01]\n",
      " [-3.06532534e-01]\n",
      " [ 8.50400710e-01]\n",
      " [ 4.90742434e-01]\n",
      " [-5.41559076e-01]\n",
      " [-5.58018218e-01]\n",
      " [-1.08691482e-01]\n",
      " [ 1.64680630e-01]\n",
      " [-1.73226754e-01]\n",
      " [ 4.16048103e-01]\n",
      " [-4.12989305e-01]\n",
      " [ 5.50785510e-01]\n",
      " [ 9.53559803e-02]\n",
      " [-2.70194693e-01]\n",
      " [ 2.57677344e-01]\n",
      " [-4.69301643e-02]\n",
      " [-3.47523864e-01]\n",
      " [ 1.07713573e-01]\n",
      " [ 6.97052302e-01]\n",
      " [-2.21140350e-01]\n",
      " [-8.29028267e-01]\n",
      " [-3.75179861e-01]\n",
      " [-1.95222539e-01]\n",
      " [ 2.89827331e-01]\n",
      " [ 4.24237424e-01]\n",
      " [ 4.44657511e-01]\n",
      " [ 1.46012692e-01]\n",
      " [-1.03098481e-01]\n",
      " [-4.24123502e-02]\n",
      " [-3.88326266e-01]\n",
      " [-4.41761487e-01]\n",
      " [ 8.92662196e-01]\n",
      " [ 5.73712187e-01]\n",
      " [-8.21423449e-01]\n",
      " [-3.21213050e-01]\n",
      " [-1.45346849e-01]\n",
      " [ 9.22272704e-01]\n",
      " [ 5.82076818e-01]\n",
      " [ 3.02579571e-01]\n",
      " [-3.43468014e-01]\n",
      " [ 4.45202340e-01]\n",
      " [-2.00316596e-01]\n",
      " [-1.15287872e-01]\n",
      " [ 2.73141467e-01]\n",
      " [ 7.67787625e-01]\n",
      " [-2.19529306e-01]\n",
      " [ 5.01680936e-01]\n",
      " [ 4.23320977e-03]\n",
      " [-1.24626303e-01]\n",
      " [-1.15385506e-01]\n",
      " [ 2.74817368e-01]\n",
      " [-3.56076500e-01]\n",
      " [ 8.15577286e-02]\n",
      " [ 5.80103456e-02]\n",
      " [ 4.18324553e-02]\n",
      " [ 1.80655708e-02]\n",
      " [-6.37024276e-01]\n",
      " [ 1.74819206e-01]\n",
      " [ 1.16581019e-01]\n",
      " [ 2.30104608e-01]\n",
      " [ 4.74740166e-01]\n",
      " [ 2.85714651e-01]\n",
      " [-8.04873550e-01]\n",
      " [-4.93841733e-01]\n",
      " [-4.74958010e-01]\n",
      " [-1.99153847e-01]\n",
      " [ 6.33885737e-03]\n",
      " [-1.23117817e-01]\n",
      " [ 1.95771208e-01]\n",
      " [-3.24136015e-01]\n",
      " [-5.01530657e-01]\n",
      " [-2.48440406e-01]\n",
      " [-6.00726867e-02]\n",
      " [ 3.92027875e-01]\n",
      " [-3.04353498e-01]\n",
      " [ 8.42224454e-01]\n",
      " [-2.67049218e-01]\n",
      " [-1.23245434e-01]\n",
      " [ 1.70996834e-01]\n",
      " [ 4.57762026e-01]\n",
      " [-3.13935960e-01]\n",
      " [-2.04311218e-01]\n",
      " [ 4.20626386e-01]\n",
      " [ 3.10270526e-01]\n",
      " [ 1.53089400e-01]\n",
      " [-4.23422876e-01]\n",
      " [-6.70752394e-01]\n",
      " [ 2.54518758e-01]\n",
      " [-2.74607813e-01]\n",
      " [-2.59391050e-01]\n",
      " [-2.13554578e-01]\n",
      " [-3.11556890e-01]\n",
      " [-2.04558840e-01]\n",
      " [-2.74342645e-01]\n",
      " [ 2.48636191e-01]\n",
      " [-2.93296725e-01]\n",
      " [-1.25754060e-02]\n",
      " [-2.43537719e-01]\n",
      " [ 2.63189407e-01]\n",
      " [ 2.21341178e-01]\n",
      " [-3.19378664e-01]\n",
      " [ 2.17708930e-01]\n",
      " [ 1.41037531e-01]\n",
      " [-8.76419825e-02]\n",
      " [ 6.43396402e-01]]\n"
     ]
    }
   ],
   "source": [
    "Regularization_Parameter = 1\n",
    "\n",
    "\n",
    "before = A.T * A+Regularization_Parameter*np.eye(cols)\n",
    "print(before.size)\n",
    "if (np.linalg.det(before)!=0):\n",
    "    print('嘿嘿,加上就可逆了,瞬间求出,看看数量级基本相同参数位置和梯度下降方法求解的是一致的')\n",
    "    theta = before.I*A.T*B \n",
    "    #θ=(x^-1X)^-1X^Ty\n",
    "    print(theta)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bebbebb2",
   "metadata": {},
   "source": [
    "## 梯度下降求解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "b92ebba0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#计算偏差值\n",
    "def computeCostWithRegularization(X,y,theta,Regularization_Parameter):\n",
    "    # 加正则化项的计算cost\n",
    "    m = len(y) #number of training examples\n",
    "    \n",
    "    inner = np.power(((X * theta) - y), 2)+ Regularization_Parameter*sum(np.power(theta, 2))\n",
    "    \n",
    "    return np.sum(inner) / (2 * m)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d46d32e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def gradientDescentWithRegularization(X, y, theta, learning_rate, num_iters, tol,print_step,Regularization_Parameter):\n",
    "    #参数说明: X数据, y预测目标值, theta参数,learinig_rate 学习率  num_iters迭代次数 tol精度(0.01)\n",
    "    m = len(y)\n",
    "    J_history = np.zeros((num_iters, 1))\n",
    "\n",
    "    for i in range(num_iters):\n",
    "        n = len(theta)\n",
    "        theta_temp = theta\n",
    "        for j in range(n):\n",
    "            theta_temp[j] = theta[j] - learning_rate/m*(X*theta-y).T*X[:,j];\n",
    "        theta = theta_temp \n",
    "        cost = computeCostWithRegularization(X, y, theta,Regularization_Parameter)\n",
    "        J_history[i] = cost\n",
    "        \n",
    "        if(i%print_step==0):\n",
    "            print('第%d次迭代, cost = %f' %(i,cost))\n",
    "        if((i>1) and (abs(J_history[i-1]-cost) < tol)): # 这里退出的判断条件改为和前一个cost 小于精确值就退出\n",
    "            print(J_history[i-1]-cost)\n",
    "            print(tol)\n",
    "            print('迭代训练结束,迭代次数:%d, 偏差值cost=%f'%(i,cost))\n",
    "            return (theta,J_history)\n",
    "    \n",
    "    return (theta,J_history)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "44d0dee3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第0次迭代, cost = 244.031603\n",
      "第10次迭代, cost = 56.621995\n",
      "第20次迭代, cost = 33.184892\n",
      "第30次迭代, cost = 31.073771\n",
      "第40次迭代, cost = 31.961966\n",
      "第50次迭代, cost = 32.928771\n",
      "第60次迭代, cost = 33.586310\n",
      "第70次迭代, cost = 33.984265\n",
      "第80次迭代, cost = 34.214863\n",
      "第90次迭代, cost = 34.346122\n",
      "第100次迭代, cost = 34.420328\n",
      "第110次迭代, cost = 34.462209\n",
      "第120次迭代, cost = 34.485865\n",
      "第130次迭代, cost = 34.499253\n",
      "[-0.00096696]\n",
      "0.001\n",
      "迭代训练结束,迭代次数:131, 偏差值cost=34.500220\n"
     ]
    }
   ],
   "source": [
    "\n",
    "A = A\n",
    "B = B\n",
    "theta = np.zeros((m,1))\n",
    "learning_rate = 0.01\n",
    "step = 2000\n",
    "tol = 0.001\n",
    "print_step = 10\n",
    "Regularization_Parameter = 1\n",
    "(theta,J_hist) = gradientDescentWithRegularization(A,B,theta,learning_rate,step,tol,print_step,Regularization_Parameter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "6bbc06e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.04666302e-01]\n",
      " [ 1.61249332e-02]\n",
      " [ 9.30553640e-02]\n",
      " [ 6.70670858e-02]\n",
      " [ 7.30796009e-01]\n",
      " [ 8.61900937e-01]\n",
      " [-3.07332789e-01]\n",
      " [-8.83860259e-03]\n",
      " [ 7.95862421e-01]\n",
      " [ 4.18091221e-01]\n",
      " [-6.31336530e-01]\n",
      " [-9.56499981e-02]\n",
      " [ 2.90437097e-01]\n",
      " [ 6.70863624e-01]\n",
      " [-2.11565216e-02]\n",
      " [ 4.93752461e-01]\n",
      " [ 1.35858406e-01]\n",
      " [-2.74904383e-01]\n",
      " [ 4.95111496e-01]\n",
      " [-4.50564692e-02]\n",
      " [ 6.08876754e-01]\n",
      " [ 4.87821477e-01]\n",
      " [ 8.09496085e-01]\n",
      " [ 7.14196400e-01]\n",
      " [-3.02412190e-01]\n",
      " [-8.29577998e-02]\n",
      " [ 1.56149271e-01]\n",
      " [ 7.17258787e-01]\n",
      " [ 3.44923772e-01]\n",
      " [ 7.10177794e-01]\n",
      " [ 4.96665399e-01]\n",
      " [ 3.81723892e-01]\n",
      " [-3.14777947e-01]\n",
      " [ 1.23004478e+00]\n",
      " [ 1.86210764e-01]\n",
      " [ 2.20831805e-02]\n",
      " [ 5.42419045e-01]\n",
      " [ 4.24329195e-01]\n",
      " [ 8.14323515e-01]\n",
      " [ 6.98341520e-01]\n",
      " [ 9.18270260e-02]\n",
      " [ 2.65105913e-02]\n",
      " [ 2.75338361e-01]\n",
      " [ 9.42550384e-01]\n",
      " [ 4.86725782e-02]\n",
      " [ 7.15104696e-01]\n",
      " [ 6.47317751e-01]\n",
      " [ 7.95648658e-02]\n",
      " [ 8.13600478e-01]\n",
      " [ 7.33785193e-01]\n",
      " [ 1.65085465e-01]\n",
      " [ 2.69820805e-01]\n",
      " [-8.61223771e-02]\n",
      " [-1.07466128e-01]\n",
      " [-3.49946205e-01]\n",
      " [ 4.87665989e-01]\n",
      " [ 1.35232811e-01]\n",
      " [-5.68195717e-01]\n",
      " [-1.07696533e-01]\n",
      " [ 3.14420727e-01]\n",
      " [-6.29893767e-03]\n",
      " [ 3.56926505e-02]\n",
      " [ 1.41083664e-02]\n",
      " [-1.03183156e+00]\n",
      " [-3.65562160e-02]\n",
      " [ 5.07320067e-01]\n",
      " [-3.61911029e-01]\n",
      " [ 4.51374616e-01]\n",
      " [-1.08985114e-01]\n",
      " [-3.09448113e-01]\n",
      " [ 2.20895630e-01]\n",
      " [-7.55821353e-02]\n",
      " [-3.71036650e-01]\n",
      " [ 6.44063718e-01]\n",
      " [-1.16149602e-01]\n",
      " [ 1.55140138e-01]\n",
      " [-2.64845382e-01]\n",
      " [-1.80355780e-02]\n",
      " [ 2.82503362e-03]\n",
      " [-1.08989850e+00]\n",
      " [ 1.40759885e-01]\n",
      " [-1.98137433e-01]\n",
      " [ 2.47082411e-01]\n",
      " [-9.84758080e-01]\n",
      " [ 1.82249751e-01]\n",
      " [-4.47129626e-02]\n",
      " [-8.80054053e-01]\n",
      " [ 8.49985571e-02]\n",
      " [ 3.82758337e-02]\n",
      " [-4.98845706e-01]\n",
      " [-3.37661442e-02]\n",
      " [ 6.56046598e-01]\n",
      " [ 1.16955417e-01]\n",
      " [-4.60614670e-01]\n",
      " [ 7.31006093e-01]\n",
      " [-1.39017481e-01]\n",
      " [ 1.81313379e-01]\n",
      " [ 5.99530109e-01]\n",
      " [-4.70406219e-01]\n",
      " [ 3.46384336e-01]\n",
      " [ 4.88887981e-01]\n",
      " [ 3.84525257e-01]\n",
      " [ 3.39468339e-01]\n",
      " [-4.63925134e-01]\n",
      " [-2.85771040e-01]\n",
      " [-7.05544749e-02]\n",
      " [-1.52075329e-01]\n",
      " [ 3.39829226e-01]\n",
      " [ 1.02918141e-01]\n",
      " [-7.79181379e-01]\n",
      " [-8.40819588e-02]\n",
      " [ 1.41701884e-01]\n",
      " [-4.59201906e-01]\n",
      " [ 7.72560098e-01]\n",
      " [-8.96444837e-02]\n",
      " [-1.09378737e-02]\n",
      " [-3.46520652e-01]\n",
      " [ 4.86808221e-01]\n",
      " [ 5.01892698e-01]\n",
      " [ 3.47468743e-01]\n",
      " [-1.28070065e-01]\n",
      " [-6.46366590e-01]\n",
      " [ 6.37705428e-02]\n",
      " [-1.54175488e-01]\n",
      " [-1.30826618e-01]\n",
      " [ 1.61352322e-01]\n",
      " [ 1.42592142e-01]\n",
      " [ 2.61195585e-02]\n",
      " [ 2.99268796e-01]\n",
      " [-1.14241641e-01]\n",
      " [-7.43336938e-02]\n",
      " [ 3.61411820e-01]\n",
      " [-6.22269324e-01]\n",
      " [-1.42907753e-01]\n",
      " [ 2.00665715e-01]\n",
      " [-9.47689504e-02]\n",
      " [-5.95505331e-01]\n",
      " [ 3.89806737e-01]\n",
      " [-2.50299455e-01]\n",
      " [-1.39071243e-01]\n",
      " [-2.01224592e-01]\n",
      " [ 3.95039854e-01]\n",
      " [-5.67087946e-01]\n",
      " [ 1.50668206e-01]\n",
      " [-2.26081959e-01]\n",
      " [ 8.70367928e-02]\n",
      " [ 5.18909176e-02]\n",
      " [-2.86133301e-01]\n",
      " [ 1.44558700e-03]\n",
      " [-4.69917197e-01]\n",
      " [ 2.79143117e-01]\n",
      " [ 1.30050566e-01]\n",
      " [ 2.69681132e-01]\n",
      " [-5.63163116e-01]\n",
      " [ 5.62224974e-02]\n",
      " [-3.28546960e-01]\n",
      " [-2.23823044e-01]\n",
      " [-1.50700783e-01]\n",
      " [ 2.00714898e-01]\n",
      " [-2.94520098e-01]\n",
      " [-2.89011620e-01]\n",
      " [ 6.56937348e-01]\n",
      " [ 2.35338756e-01]\n",
      " [-2.14949358e-01]\n",
      " [ 5.13813739e-01]\n",
      " [ 2.68627865e-01]\n",
      " [ 2.33521223e-01]\n",
      " [-3.87582170e-01]\n",
      " [ 3.76953112e-01]\n",
      " [ 1.80497138e-01]\n",
      " [ 1.79798232e-01]\n",
      " [-4.47697097e-01]\n",
      " [ 9.52882105e-03]\n",
      " [ 2.47858796e-01]\n",
      " [-8.49676850e-01]\n",
      " [-1.45461325e-01]\n",
      " [-4.21810954e-01]\n",
      " [-4.70703510e-01]\n",
      " [ 4.10390066e-01]\n",
      " [-1.21282704e-02]\n",
      " [-4.48624048e-01]\n",
      " [-2.24683520e-01]\n",
      " [ 6.18704971e-04]\n",
      " [-5.75177421e-02]\n",
      " [ 1.40604121e-01]\n",
      " [-2.45689725e-02]\n",
      " [-3.52123127e-01]\n",
      " [ 8.71646343e-02]\n",
      " [ 4.82046831e-01]\n",
      " [ 1.25025764e-01]\n",
      " [-4.57222626e-01]\n",
      " [-2.39471752e-01]\n",
      " [ 3.56439322e-01]\n",
      " [-8.00572786e-03]\n",
      " [-2.95599698e-01]\n",
      " [-1.65353141e-01]\n",
      " [ 4.44845670e-01]\n",
      " [ 1.00386665e-01]\n",
      " [ 1.60546962e-02]\n",
      " [ 4.81415986e-02]\n",
      " [-5.13732592e-03]\n",
      " [-2.10115954e-01]\n",
      " [ 1.85505572e-01]\n",
      " [ 1.62061986e-01]\n",
      " [ 3.80678221e-01]\n",
      " [ 3.57834170e-01]\n",
      " [-5.21204460e-01]\n",
      " [-1.89046861e-01]\n",
      " [-4.13190707e-01]\n",
      " [-2.36752501e-01]\n",
      " [-1.68572303e-01]\n",
      " [ 5.42358235e-02]\n",
      " [ 3.48644168e-01]\n",
      " [-1.08623858e-01]\n",
      " [-3.40474539e-01]\n",
      " [-9.81352356e-02]\n",
      " [ 4.28117992e-01]\n",
      " [ 4.83526408e-01]\n",
      " [-2.92311650e-01]\n",
      " [ 8.76232173e-01]\n",
      " [-1.05548015e-01]\n",
      " [-3.60557120e-01]\n",
      " [-1.62125083e-01]\n",
      " [-3.50309892e-01]\n",
      " [-4.30939672e-02]\n",
      " [-5.07048670e-03]\n",
      " [ 7.03824642e-02]\n",
      " [-2.71888161e-01]\n",
      " [-2.83436688e-02]\n",
      " [-5.71173998e-02]\n",
      " [ 1.86152735e-01]\n",
      " [ 2.13956853e-01]\n",
      " [-4.80364525e-01]\n",
      " [ 3.84277929e-01]\n",
      " [ 3.62812708e-02]\n",
      " [-4.10746427e-01]\n",
      " [-3.19800433e-01]\n",
      " [ 5.41147333e-01]\n",
      " [-4.11814142e-01]\n",
      " [-7.07409552e-01]\n",
      " [ 7.07952575e-02]\n",
      " [ 1.36765069e-01]\n",
      " [-5.32306555e-01]\n",
      " [-1.58341550e-01]\n",
      " [-5.92620753e-01]\n",
      " [-5.33794312e-01]\n",
      " [-6.63850609e-02]\n",
      " [ 2.48713407e-01]\n",
      " [-8.91564561e-02]\n",
      " [ 8.62637457e-01]\n",
      " [ 2.66033283e-01]\n",
      " [-7.15653743e-02]\n",
      " [ 5.67889616e-01]\n",
      " [ 4.59103745e-01]\n",
      " [ 4.56261706e-01]\n",
      " [-4.26744622e-02]\n",
      " [ 4.64226583e-01]\n",
      " [-5.27347943e-03]\n",
      " [ 1.52922440e-01]\n",
      " [-4.95403462e-01]\n",
      " [-8.26295885e-02]\n",
      " [ 6.06172102e-01]\n",
      " [-6.72113371e-02]\n",
      " [-1.36316767e-01]\n",
      " [ 6.15657070e-02]\n",
      " [ 1.00875717e-01]\n",
      " [ 1.56881072e-01]\n",
      " [ 6.13138146e-01]\n",
      " [ 1.17689476e-01]\n",
      " [-1.31928401e-01]\n",
      " [-8.64923684e-01]\n",
      " [-2.51101677e-01]\n",
      " [ 1.58544825e-01]\n",
      " [ 3.00083010e-01]\n",
      " [ 1.38121938e-01]\n",
      " [ 3.22003611e-01]\n",
      " [-1.45300396e-01]\n",
      " [ 1.52609314e-01]\n",
      " [-2.00435537e-02]\n",
      " [ 1.47844827e-01]\n",
      " [ 3.90713741e-01]\n",
      " [-3.80091172e-01]\n",
      " [-3.48341208e-01]\n",
      " [ 9.54455718e-02]\n",
      " [ 3.25387833e-01]\n",
      " [-1.17957438e-01]\n",
      " [ 9.27176161e-03]\n",
      " [ 3.16530840e-01]\n",
      " [ 3.26417676e-01]\n",
      " [-2.79459063e-01]\n",
      " [ 2.16879569e-01]\n",
      " [-3.90531244e-02]\n",
      " [-5.19889468e-01]\n",
      " [-2.09911537e-01]\n",
      " [ 1.65792419e-01]\n",
      " [ 4.82088724e-01]\n",
      " [-9.73571018e-02]\n",
      " [ 2.79278111e-01]\n",
      " [-6.90398688e-02]\n",
      " [-6.79716048e-01]\n",
      " [ 2.30122943e-02]\n",
      " [-2.20757272e-01]\n",
      " [ 7.60743204e-02]\n",
      " [ 2.64813417e-01]\n",
      " [-1.62157220e-01]\n",
      " [ 1.08829603e-01]\n",
      " [ 3.03529726e-01]\n",
      " [-1.60002409e-01]\n",
      " [-2.53612509e-01]\n",
      " [ 4.81248306e-01]\n",
      " [ 1.36815185e-02]\n",
      " [-8.73307610e-02]\n",
      " [-1.10424255e-01]\n",
      " [ 1.47556826e-02]\n",
      " [-9.35581639e-01]\n",
      " [ 1.62958223e-01]\n",
      " [ 7.05683319e-01]\n",
      " [ 5.13554886e-02]\n",
      " [ 7.88358248e-04]\n",
      " [-3.14528015e-01]\n",
      " [-4.03849944e-01]\n",
      " [-4.03612060e-01]\n",
      " [ 6.92534611e-01]\n",
      " [-3.35081093e-01]\n",
      " [-3.39882572e-01]\n",
      " [-1.13811150e-01]\n",
      " [ 1.21216624e-01]\n",
      " [-4.08233745e-01]\n",
      " [ 1.58420927e-01]\n",
      " [ 1.11351663e-01]\n",
      " [ 4.78599074e-02]\n",
      " [-6.00025415e-01]\n",
      " [ 6.52839575e-02]\n",
      " [ 2.33101168e-01]\n",
      " [ 6.04790026e-01]\n",
      " [ 3.32452463e-01]\n",
      " [ 7.72503749e-02]\n",
      " [ 2.43764296e-02]\n",
      " [-9.89004992e-02]\n",
      " [ 3.78195386e-01]\n",
      " [-4.22076264e-01]\n",
      " [-1.46323355e-01]\n",
      " [-8.58830561e-02]\n",
      " [-1.19725617e-01]\n",
      " [-2.07822245e-02]\n",
      " [ 5.85419674e-02]\n",
      " [ 9.05015020e-02]\n",
      " [-1.96633642e-01]\n",
      " [ 3.56169989e-02]\n",
      " [-8.80671751e-02]\n",
      " [-9.95511011e-02]\n",
      " [-3.28222387e-01]\n",
      " [ 5.28180782e-01]\n",
      " [-3.07013847e-01]\n",
      " [-1.69558752e-02]\n",
      " [ 2.09432816e-01]\n",
      " [-7.36993872e-01]\n",
      " [-1.79794231e-01]\n",
      " [-2.08775582e-01]\n",
      " [-3.13720317e-01]\n",
      " [-5.42858865e-01]\n",
      " [-1.82158659e-01]\n",
      " [ 3.87730958e-01]\n",
      " [ 2.12363319e-01]\n",
      " [-1.24174889e-01]\n",
      " [ 9.18166674e-02]\n",
      " [ 1.62559838e-01]\n",
      " [ 6.34476723e-02]\n",
      " [ 3.47252227e-01]\n",
      " [-1.87121488e-01]\n",
      " [-5.35220759e-01]\n",
      " [-2.14365846e-01]\n",
      " [-1.15795013e-01]\n",
      " [-4.71072147e-01]\n",
      " [ 3.26336080e-01]\n",
      " [ 4.77855169e-01]\n",
      " [ 2.02268440e-02]\n",
      " [-5.87037532e-01]\n",
      " [-4.95496255e-02]\n",
      " [-9.94763503e-02]\n",
      " [-1.66814806e-01]\n",
      " [ 1.48273549e-01]\n",
      " [-3.36966352e-01]\n",
      " [-2.18621106e-01]\n",
      " [-3.17252148e-01]\n",
      " [ 2.42674718e-01]\n",
      " [ 3.32993103e-01]\n",
      " [-1.21030815e-01]\n",
      " [ 3.44099637e-01]\n",
      " [ 2.60606937e-01]\n",
      " [ 9.13197105e-02]\n",
      " [ 2.81619005e-01]\n",
      " [ 1.25322262e-01]\n",
      " [-5.65070215e-01]\n",
      " [ 1.60460037e-01]\n",
      " [ 2.28724231e-01]\n",
      " [-1.55487050e-01]\n",
      " [-2.94972512e-01]\n",
      " [ 8.30309495e-01]\n",
      " [ 4.86776737e-01]\n",
      " [-5.28059403e-01]\n",
      " [-5.55522327e-01]\n",
      " [-1.06831403e-01]\n",
      " [ 1.67174291e-01]\n",
      " [-1.61927388e-01]\n",
      " [ 4.03101037e-01]\n",
      " [-4.09998652e-01]\n",
      " [ 5.42770500e-01]\n",
      " [ 9.54561955e-02]\n",
      " [-2.62802718e-01]\n",
      " [ 2.44096780e-01]\n",
      " [-4.99466562e-02]\n",
      " [-3.37927125e-01]\n",
      " [ 1.03397005e-01]\n",
      " [ 6.80365115e-01]\n",
      " [-2.14454113e-01]\n",
      " [-8.14318091e-01]\n",
      " [-3.66817784e-01]\n",
      " [-1.80460095e-01]\n",
      " [ 2.79865667e-01]\n",
      " [ 4.09967839e-01]\n",
      " [ 4.34589135e-01]\n",
      " [ 1.36706385e-01]\n",
      " [-1.04091321e-01]\n",
      " [-4.56497620e-02]\n",
      " [-3.82448573e-01]\n",
      " [-4.25431538e-01]\n",
      " [ 8.77337827e-01]\n",
      " [ 5.58290664e-01]\n",
      " [-8.00386684e-01]\n",
      " [-3.20336207e-01]\n",
      " [-1.45878857e-01]\n",
      " [ 9.02821888e-01]\n",
      " [ 5.63465076e-01]\n",
      " [ 2.96134421e-01]\n",
      " [-3.38776582e-01]\n",
      " [ 4.37026141e-01]\n",
      " [-1.94280037e-01]\n",
      " [-1.11238135e-01]\n",
      " [ 2.66339538e-01]\n",
      " [ 7.44994926e-01]\n",
      " [-2.16961777e-01]\n",
      " [ 4.89183574e-01]\n",
      " [ 1.00079559e-02]\n",
      " [-1.16647891e-01]\n",
      " [-1.12839098e-01]\n",
      " [ 2.67779266e-01]\n",
      " [-3.44968310e-01]\n",
      " [ 7.54524103e-02]\n",
      " [ 5.24507659e-02]\n",
      " [ 4.14358605e-02]\n",
      " [ 2.06046099e-02]\n",
      " [-6.11814495e-01]\n",
      " [ 1.78507104e-01]\n",
      " [ 1.10850211e-01]\n",
      " [ 2.16250148e-01]\n",
      " [ 4.59601143e-01]\n",
      " [ 2.73170625e-01]\n",
      " [-7.75519357e-01]\n",
      " [-4.68905608e-01]\n",
      " [-4.51969473e-01]\n",
      " [-2.05442601e-01]\n",
      " [ 1.11677880e-02]\n",
      " [-1.17607481e-01]\n",
      " [ 1.88019221e-01]\n",
      " [-2.99362678e-01]\n",
      " [-4.90310006e-01]\n",
      " [-2.38433480e-01]\n",
      " [-5.73696749e-02]\n",
      " [ 3.78102464e-01]\n",
      " [-2.95277448e-01]\n",
      " [ 8.25694373e-01]\n",
      " [-2.54263595e-01]\n",
      " [-1.29694741e-01]\n",
      " [ 1.60792638e-01]\n",
      " [ 4.52789302e-01]\n",
      " [-3.04885533e-01]\n",
      " [-1.90126010e-01]\n",
      " [ 4.03038631e-01]\n",
      " [ 3.01956154e-01]\n",
      " [ 1.39091054e-01]\n",
      " [-4.17914887e-01]\n",
      " [-6.48993983e-01]\n",
      " [ 2.37689272e-01]\n",
      " [-2.72767768e-01]\n",
      " [-2.49110322e-01]\n",
      " [-2.07596174e-01]\n",
      " [-3.01832499e-01]\n",
      " [-1.96901370e-01]\n",
      " [-2.62261411e-01]\n",
      " [ 2.38117346e-01]\n",
      " [-2.88858085e-01]\n",
      " [-1.64285250e-02]\n",
      " [-2.40186323e-01]\n",
      " [ 2.47292285e-01]\n",
      " [ 2.12855911e-01]\n",
      " [-3.14396658e-01]\n",
      " [ 1.98277281e-01]\n",
      " [ 1.31812742e-01]\n",
      " [-8.30108126e-02]\n",
      " [ 6.15280982e-01]]\n"
     ]
    }
   ],
   "source": [
    "print(theta)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "b5599c02",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1a3727ed108>]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAajUlEQVR4nO3dfZAc9Z3f8fd3nvZJu9qVdiWklWCFLDAyGKHsyWADsYOxAfssO7m6QChbOZPSpYLrjOOqK7CT+Fwpp5zLnR2o2CQ4cOZylG2COYM5cg5WqAB2wF5hEHowSEYSekJakLQrtLva2Zlv/uie1Wi1i/Zptme6P6+qqen+dc/0Vy3tZ3/6dc9vzN0REZF4SUVdgIiIzD6Fu4hIDCncRURiSOEuIhJDCncRkRjKRF0AQHt7u3d1dUVdhohITdm8efNb7t4x3raqCPeuri56enqiLkNEpK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4B5AlC6LaJzhnBRfTvhD87rxDc+RNVjbsIxq5LPzP/tWz/r4Y1vgrcGEV9Y7bv4fQF1UjO4WQfmn5ARCSGanlYRkREJqBwFxGJIYW7iEgMKdxFRGJI4S4iEkMKdxGRGFK4i4jE0P8Hxd0uIYnNUAIAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "print(J_hist.size)\n",
    "x_axis = np.linspace(0,150,150)\n",
    "plt.plot(x_axis,J_hist[0:150]) # "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4643ea56",
   "metadata": {},
   "source": [
    "# 2.\t对randn_data_regression_A的某一列乘上一个很大的数（比如100000），再调用梯度下降法计算线性回归模型的参数，你有什么发现？你觉得如何处理才有可能降低梯度下降法的迭代步数？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a0fbc3f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "\n",
    "# 这里是求解 方法二\n",
    "\n",
    "# 第一部分: 设定传入参数\n",
    "n = rows\n",
    "m = cols\n",
    "X0 = theta\n",
    "\n",
    "A1 = np.array(A, copy=True)\n",
    "A1[:,2] = A1[:,2]*100\n",
    "A1 = mat(A1)\n",
    "print(A)\n",
    "print(A1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "821684da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第0次迭代, cost = 293.981012\n",
      "第10次迭代, cost = 277.063735\n",
      "第20次迭代, cost = 261.442090\n",
      "第30次迭代, cost = 246.808642\n",
      "第40次迭代, cost = 233.094711\n",
      "第50次迭代, cost = 220.236776\n",
      "第60次迭代, cost = 208.176025\n",
      "第70次迭代, cost = 196.857988\n",
      "第80次迭代, cost = 186.232200\n",
      "第90次迭代, cost = 176.251893\n",
      "第100次迭代, cost = 166.873704\n",
      "第110次迭代, cost = 158.057421\n",
      "第120次迭代, cost = 149.765733\n",
      "第130次迭代, cost = 141.964014\n",
      "第140次迭代, cost = 134.620115\n",
      "第150次迭代, cost = 127.704180\n",
      "第160次迭代, cost = 121.188467\n",
      "第170次迭代, cost = 115.047192\n",
      "第180次迭代, cost = 109.256381\n",
      "第190次迭代, cost = 103.793735\n",
      "第200次迭代, cost = 98.638504\n",
      "第210次迭代, cost = 93.771374\n",
      "第220次迭代, cost = 89.174359\n",
      "第230次迭代, cost = 84.830704\n",
      "第240次迭代, cost = 80.724794\n",
      "第250次迭代, cost = 76.842072\n",
      "第260次迭代, cost = 73.168959\n",
      "第270次迭代, cost = 69.692787\n",
      "第280次迭代, cost = 66.401733\n",
      "第290次迭代, cost = 63.284753\n",
      "第300次迭代, cost = 60.331533\n",
      "第310次迭代, cost = 57.532435\n",
      "第320次迭代, cost = 54.878445\n",
      "第330次迭代, cost = 52.361135\n",
      "第340次迭代, cost = 49.972619\n",
      "第350次迭代, cost = 47.705514\n",
      "第360次迭代, cost = 45.552907\n",
      "第370次迭代, cost = 43.508323\n",
      "第380次迭代, cost = 41.565691\n",
      "第390次迭代, cost = 39.719323\n",
      "第400次迭代, cost = 37.963880\n",
      "第410次迭代, cost = 36.294354\n",
      "第420次迭代, cost = 34.706044\n",
      "第430次迭代, cost = 33.194535\n",
      "第440次迭代, cost = 31.755680\n",
      "第450次迭代, cost = 30.385580\n",
      "第460次迭代, cost = 29.080570\n",
      "第470次迭代, cost = 27.837203\n",
      "第480次迭代, cost = 26.652235\n",
      "第490次迭代, cost = 25.522612\n",
      "第500次迭代, cost = 24.445459\n",
      "第510次迭代, cost = 23.418067\n",
      "第520次迭代, cost = 22.437881\n",
      "第530次迭代, cost = 21.502496\n",
      "第540次迭代, cost = 20.609640\n",
      "第550次迭代, cost = 19.757171\n",
      "第560次迭代, cost = 18.943068\n",
      "第570次迭代, cost = 18.165421\n",
      "第580次迭代, cost = 17.422427\n",
      "第590次迭代, cost = 16.712381\n",
      "第600次迭代, cost = 16.033672\n",
      "第610次迭代, cost = 15.384777\n",
      "第620次迭代, cost = 14.764256\n",
      "第630次迭代, cost = 14.170744\n",
      "第640次迭代, cost = 13.602950\n",
      "第650次迭代, cost = 13.059652\n",
      "第660次迭代, cost = 12.539693\n",
      "第670次迭代, cost = 12.041974\n",
      "第680次迭代, cost = 11.565456\n",
      "第690次迭代, cost = 11.109152\n",
      "第700次迭代, cost = 10.672127\n",
      "第710次迭代, cost = 10.253492\n",
      "第720次迭代, cost = 9.852407\n",
      "第730次迭代, cost = 9.468070\n",
      "第740次迭代, cost = 9.099721\n",
      "第750次迭代, cost = 8.746640\n",
      "第760次迭代, cost = 8.408140\n",
      "第770次迭代, cost = 8.083569\n",
      "第780次迭代, cost = 7.772307\n",
      "第790次迭代, cost = 7.473765\n",
      "第800次迭代, cost = 7.187382\n",
      "第810次迭代, cost = 6.912623\n",
      "第820次迭代, cost = 6.648980\n",
      "第830次迭代, cost = 6.395970\n",
      "第840次迭代, cost = 6.153132\n",
      "第850次迭代, cost = 5.920026\n",
      "第860次迭代, cost = 5.696235\n",
      "第870次迭代, cost = 5.481359\n",
      "第880次迭代, cost = 5.275017\n",
      "第890次迭代, cost = 5.076849\n",
      "第900次迭代, cost = 4.886507\n",
      "第910次迭代, cost = 4.703662\n",
      "第920次迭代, cost = 4.527999\n",
      "第930次迭代, cost = 4.359218\n",
      "第940次迭代, cost = 4.197031\n",
      "第950次迭代, cost = 4.041164\n",
      "第960次迭代, cost = 3.891356\n",
      "第970次迭代, cost = 3.747357\n",
      "第980次迭代, cost = 3.608928\n",
      "第990次迭代, cost = 3.475840\n",
      "第1000次迭代, cost = 3.347876\n",
      "第1010次迭代, cost = 3.224826\n",
      "第1020次迭代, cost = 3.106491\n",
      "第1030次迭代, cost = 2.992681\n",
      "第1040次迭代, cost = 2.883213\n",
      "第1050次迭代, cost = 2.777912\n",
      "第1060次迭代, cost = 2.676611\n",
      "第1070次迭代, cost = 2.579149\n",
      "第1080次迭代, cost = 2.485375\n",
      "第1090次迭代, cost = 2.395141\n",
      "第1100次迭代, cost = 2.308307\n",
      "第1110次迭代, cost = 2.224738\n",
      "第1120次迭代, cost = 2.144306\n",
      "第1130次迭代, cost = 2.066886\n",
      "第1140次迭代, cost = 1.992362\n",
      "第1150次迭代, cost = 1.920619\n",
      "第1160次迭代, cost = 1.851550\n",
      "第1170次迭代, cost = 1.785049\n",
      "第1180次迭代, cost = 1.721017\n",
      "第1190次迭代, cost = 1.659358\n",
      "第1200次迭代, cost = 1.599981\n",
      "第1210次迭代, cost = 1.542797\n",
      "第1220次迭代, cost = 1.487722\n",
      "第1230次迭代, cost = 1.434675\n",
      "第1240次迭代, cost = 1.383578\n",
      "第1250次迭代, cost = 1.334357\n",
      "第1260次迭代, cost = 1.286940\n",
      "第1270次迭代, cost = 1.241258\n",
      "第1280次迭代, cost = 1.197245\n",
      "第1290次迭代, cost = 1.154838\n",
      "第1300次迭代, cost = 1.113977\n",
      "第1310次迭代, cost = 1.074602\n",
      "第1320次迭代, cost = 1.036657\n",
      "第1330次迭代, cost = 1.000090\n",
      "第1340次迭代, cost = 0.964847\n",
      "第1350次迭代, cost = 0.930880\n",
      "第1360次迭代, cost = 0.898141\n",
      "第1370次迭代, cost = 0.866583\n",
      "第1380次迭代, cost = 0.836162\n",
      "第1390次迭代, cost = 0.806837\n",
      "第1400次迭代, cost = 0.778567\n",
      "第1410次迭代, cost = 0.751311\n",
      "第1420次迭代, cost = 0.725033\n",
      "第1430次迭代, cost = 0.699697\n",
      "第1440次迭代, cost = 0.675268\n",
      "第1450次迭代, cost = 0.651711\n",
      "第1460次迭代, cost = 0.628996\n",
      "第1470次迭代, cost = 0.607091\n",
      "第1480次迭代, cost = 0.585966\n",
      "第1490次迭代, cost = 0.565593\n",
      "第1500次迭代, cost = 0.545944\n",
      "第1510次迭代, cost = 0.526993\n",
      "第1520次迭代, cost = 0.508715\n",
      "第1530次迭代, cost = 0.491084\n",
      "第1540次迭代, cost = 0.474077\n",
      "第1550次迭代, cost = 0.457671\n",
      "第1560次迭代, cost = 0.441846\n",
      "第1570次迭代, cost = 0.426578\n",
      "第1580次迭代, cost = 0.411850\n",
      "第1590次迭代, cost = 0.397640\n",
      "第1600次迭代, cost = 0.383930\n",
      "第1610次迭代, cost = 0.370702\n",
      "第1620次迭代, cost = 0.357939\n",
      "第1630次迭代, cost = 0.345624\n",
      "第1640次迭代, cost = 0.333740\n",
      "第1650次迭代, cost = 0.322274\n",
      "第1660次迭代, cost = 0.311208\n",
      "第1670次迭代, cost = 0.300529\n",
      "第1680次迭代, cost = 0.290224\n",
      "第1690次迭代, cost = 0.280279\n",
      "第1700次迭代, cost = 0.270680\n",
      "第1710次迭代, cost = 0.261416\n",
      "第1720次迭代, cost = 0.252475\n",
      "第1730次迭代, cost = 0.243845\n",
      "第1740次迭代, cost = 0.235515\n",
      "第1750次迭代, cost = 0.227474\n",
      "第1760次迭代, cost = 0.219713\n",
      "第1770次迭代, cost = 0.212221\n",
      "第1780次迭代, cost = 0.204988\n",
      "第1790次迭代, cost = 0.198006\n",
      "第1800次迭代, cost = 0.191266\n",
      "第1810次迭代, cost = 0.184759\n",
      "第1820次迭代, cost = 0.178477\n",
      "第1830次迭代, cost = 0.172412\n",
      "第1840次迭代, cost = 0.166556\n",
      "第1850次迭代, cost = 0.160903\n",
      "第1860次迭代, cost = 0.155444\n",
      "第1870次迭代, cost = 0.150173\n",
      "第1880次迭代, cost = 0.145083\n",
      "第1890次迭代, cost = 0.140169\n",
      "第1900次迭代, cost = 0.135424\n",
      "第1910次迭代, cost = 0.130841\n",
      "第1920次迭代, cost = 0.126416\n",
      "第1930次迭代, cost = 0.122143\n",
      "第1940次迭代, cost = 0.118016\n",
      "第1950次迭代, cost = 0.114031\n",
      "第1960次迭代, cost = 0.110182\n",
      "第1970次迭代, cost = 0.106465\n",
      "第1980次迭代, cost = 0.102875\n",
      "第1990次迭代, cost = 0.099408\n",
      "第2000次迭代, cost = 0.096059\n",
      "第2010次迭代, cost = 0.092824\n",
      "第2020次迭代, cost = 0.089700\n",
      "第2030次迭代, cost = 0.086683\n",
      "第2040次迭代, cost = 0.083768\n",
      "第2050次迭代, cost = 0.080952\n",
      "第2060次迭代, cost = 0.078233\n",
      "第2070次迭代, cost = 0.075606\n",
      "第2080次迭代, cost = 0.073068\n",
      "第2090次迭代, cost = 0.070616\n",
      "第2100次迭代, cost = 0.068248\n",
      "第2110次迭代, cost = 0.065960\n",
      "第2120次迭代, cost = 0.063750\n",
      "第2130次迭代, cost = 0.061615\n",
      "第2140次迭代, cost = 0.059552\n",
      "第2150次迭代, cost = 0.057559\n",
      "第2160次迭代, cost = 0.055634\n",
      "第2170次迭代, cost = 0.053773\n",
      "第2180次迭代, cost = 0.051976\n",
      "第2190次迭代, cost = 0.050239\n",
      "第2200次迭代, cost = 0.048561\n",
      "第2210次迭代, cost = 0.046940\n",
      "第2220次迭代, cost = 0.045374\n",
      "第2230次迭代, cost = 0.043860\n",
      "第2240次迭代, cost = 0.042397\n",
      "第2250次迭代, cost = 0.040984\n",
      "第2260次迭代, cost = 0.039619\n",
      "第2270次迭代, cost = 0.038299\n",
      "第2280次迭代, cost = 0.037024\n",
      "第2290次迭代, cost = 0.035791\n",
      "第2300次迭代, cost = 0.034601\n",
      "第2310次迭代, cost = 0.033450\n",
      "第2320次迭代, cost = 0.032338\n",
      "第2330次迭代, cost = 0.031263\n",
      "第2340次迭代, cost = 0.030224\n",
      "第2350次迭代, cost = 0.029220\n",
      "第2360次迭代, cost = 0.028250\n",
      "第2370次迭代, cost = 0.027313\n",
      "第2380次迭代, cost = 0.026406\n",
      "第2390次迭代, cost = 0.025531\n",
      "第2400次迭代, cost = 0.024684\n",
      "第2410次迭代, cost = 0.023866\n",
      "第2420次迭代, cost = 0.023075\n",
      "第2430次迭代, cost = 0.022311\n",
      "第2440次迭代, cost = 0.021572\n",
      "第2450次迭代, cost = 0.020858\n",
      "第2460次迭代, cost = 0.020168\n",
      "第2470次迭代, cost = 0.019501\n",
      "第2480次迭代, cost = 0.018856\n",
      "第2490次迭代, cost = 0.018232\n",
      "第2500次迭代, cost = 0.017630\n",
      "第2510次迭代, cost = 0.017047\n",
      "第2520次迭代, cost = 0.016484\n",
      "第2530次迭代, cost = 0.015940\n",
      "第2540次迭代, cost = 0.015414\n",
      "第2550次迭代, cost = 0.014905\n",
      "第2560次迭代, cost = 0.014414\n",
      "第2570次迭代, cost = 0.013938\n",
      "第2580次迭代, cost = 0.013479\n",
      "第2590次迭代, cost = 0.013034\n",
      "第2600次迭代, cost = 0.012605\n",
      "第2610次迭代, cost = 0.012190\n",
      "第2620次迭代, cost = 0.011788\n",
      "第2630次迭代, cost = 0.011400\n",
      "第2640次迭代, cost = 0.011025\n",
      "第2650次迭代, cost = 0.010662\n",
      "第2660次迭代, cost = 0.010312\n",
      "第2670次迭代, cost = 0.009973\n",
      "第2680次迭代, cost = 0.009645\n",
      "第2690次迭代, cost = 0.009328\n",
      "第2700次迭代, cost = 0.009021\n",
      "第2710次迭代, cost = 0.008725\n",
      "第2720次迭代, cost = 0.008438\n",
      "第2730次迭代, cost = 0.008161\n",
      "第2740次迭代, cost = 0.007893\n",
      "第2750次迭代, cost = 0.007634\n",
      "第2760次迭代, cost = 0.007384\n",
      "第2770次迭代, cost = 0.007142\n",
      "第2780次迭代, cost = 0.006908\n",
      "第2790次迭代, cost = 0.006681\n",
      "第2800次迭代, cost = 0.006462\n",
      "第2810次迭代, cost = 0.006251\n",
      "第2820次迭代, cost = 0.006046\n",
      "第2830次迭代, cost = 0.005848\n",
      "第2840次迭代, cost = 0.005656\n",
      "第2850次迭代, cost = 0.005471\n",
      "第2860次迭代, cost = 0.005292\n",
      "第2870次迭代, cost = 0.005119\n",
      "第2880次迭代, cost = 0.004952\n",
      "第2890次迭代, cost = 0.004790\n",
      "第2900次迭代, cost = 0.004633\n",
      "第2910次迭代, cost = 0.004482\n",
      "第2920次迭代, cost = 0.004335\n",
      "第2930次迭代, cost = 0.004194\n",
      "第2940次迭代, cost = 0.004057\n",
      "第2950次迭代, cost = 0.003924\n",
      "第2960次迭代, cost = 0.003796\n",
      "第2970次迭代, cost = 0.003672\n",
      "第2980次迭代, cost = 0.003552\n",
      "第2990次迭代, cost = 0.003436\n",
      "第3000次迭代, cost = 0.003324\n",
      "第3010次迭代, cost = 0.003216\n",
      "第3020次迭代, cost = 0.003111\n",
      "第3030次迭代, cost = 0.003010\n",
      "第3040次迭代, cost = 0.002912\n",
      "第3050次迭代, cost = 0.002817\n",
      "第3060次迭代, cost = 0.002725\n",
      "第3070次迭代, cost = 0.002636\n",
      "第3080次迭代, cost = 0.002550\n",
      "第3090次迭代, cost = 0.002467\n",
      "第3100次迭代, cost = 0.002387\n",
      "第3110次迭代, cost = 0.002309\n",
      "第3120次迭代, cost = 0.002234\n",
      "第3130次迭代, cost = 0.002162\n",
      "第3140次迭代, cost = 0.002091\n",
      "第3150次迭代, cost = 0.002023\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第3160次迭代, cost = 0.001958\n",
      "第3170次迭代, cost = 0.001894\n",
      "第3180次迭代, cost = 0.001832\n",
      "第3190次迭代, cost = 0.001773\n",
      "第3200次迭代, cost = 0.001715\n",
      "第3210次迭代, cost = 0.001660\n",
      "第3220次迭代, cost = 0.001606\n",
      "第3230次迭代, cost = 0.001554\n",
      "第3240次迭代, cost = 0.001503\n",
      "第3250次迭代, cost = 0.001454\n",
      "第3260次迭代, cost = 0.001407\n",
      "第3270次迭代, cost = 0.001362\n",
      "第3280次迭代, cost = 0.001317\n",
      "第3290次迭代, cost = 0.001275\n",
      "第3300次迭代, cost = 0.001233\n",
      "第3310次迭代, cost = 0.001193\n",
      "第3320次迭代, cost = 0.001155\n",
      "第3330次迭代, cost = 0.001117\n",
      "第3340次迭代, cost = 0.001081\n",
      "第3350次迭代, cost = 0.001046\n",
      "第3360次迭代, cost = 0.001012\n",
      "迭代训练结束,迭代次数:3364, 偏差值cost=0.000999\n"
     ]
    }
   ],
   "source": [
    "\n",
    "B = B\n",
    "theta = np.zeros((m,1))\n",
    "learning_rate = 0.0003 #经过调整这个rate 大小比较合适 0.001太大 0.0001 太慢 0.0003算快\n",
    "step = 5000\n",
    "tol = 0.001\n",
    "print_step = 10\n",
    "\n",
    "# 梯度下降方法\n",
    "(theta,J_hist) = gradientDescent(A1,B,theta,learning_rate,step,tol,print_step)\n",
    "#print(theta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "865f1909",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.17920398]\n",
      " [ 0.03769754]\n",
      " [ 0.01571062]\n",
      " [ 0.04968849]\n",
      " [ 0.70920993]\n",
      " [ 0.84338537]\n",
      " [-0.29813319]\n",
      " [ 0.0050518 ]\n",
      " [ 0.76243362]\n",
      " [ 0.40694364]\n",
      " [-0.60218219]\n",
      " [-0.08743242]\n",
      " [ 0.28807187]\n",
      " [ 0.66062214]\n",
      " [-0.01078813]\n",
      " [ 0.51855851]\n",
      " [ 0.12769197]\n",
      " [-0.25315371]\n",
      " [ 0.45996678]\n",
      " [-0.03056517]\n",
      " [ 0.57548325]\n",
      " [ 0.47761665]\n",
      " [ 0.79753737]\n",
      " [ 0.68392113]\n",
      " [-0.26335685]\n",
      " [-0.07038375]\n",
      " [ 0.14161191]\n",
      " [ 0.71086982]\n",
      " [ 0.30632122]\n",
      " [ 0.68510775]\n",
      " [ 0.4908743 ]\n",
      " [ 0.37464192]\n",
      " [-0.30266095]\n",
      " [ 1.15315609]\n",
      " [ 0.15387126]\n",
      " [ 0.03387228]\n",
      " [ 0.52566686]\n",
      " [ 0.41576726]\n",
      " [ 0.80560635]\n",
      " [ 0.69002599]\n",
      " [ 0.05197054]\n",
      " [ 0.00256392]\n",
      " [ 0.25828691]\n",
      " [ 0.88249992]\n",
      " [ 0.03498854]\n",
      " [ 0.6703486 ]\n",
      " [ 0.64900487]\n",
      " [ 0.07811154]\n",
      " [ 0.80793892]\n",
      " [ 0.69239405]\n",
      " [ 0.16522308]\n",
      " [ 0.2354061 ]\n",
      " [-0.09541889]\n",
      " [-0.10259306]\n",
      " [-0.33261736]\n",
      " [ 0.46629166]\n",
      " [ 0.11958703]\n",
      " [-0.54903185]\n",
      " [-0.11157978]\n",
      " [ 0.28007356]\n",
      " [-0.01450945]\n",
      " [ 0.05192058]\n",
      " [ 0.02037394]\n",
      " [-1.00030843]\n",
      " [-0.04704326]\n",
      " [ 0.51294499]\n",
      " [-0.34947741]\n",
      " [ 0.45627982]\n",
      " [-0.10020799]\n",
      " [-0.33116585]\n",
      " [ 0.2128251 ]\n",
      " [-0.06725971]\n",
      " [-0.36944424]\n",
      " [ 0.6190917 ]\n",
      " [-0.10043096]\n",
      " [ 0.13570911]\n",
      " [-0.27048303]\n",
      " [-0.03593411]\n",
      " [-0.01681811]\n",
      " [-1.0830398 ]\n",
      " [ 0.12588274]\n",
      " [-0.20417797]\n",
      " [ 0.21544595]\n",
      " [-0.96925907]\n",
      " [ 0.18122938]\n",
      " [-0.03122606]\n",
      " [-0.8972488 ]\n",
      " [ 0.07724504]\n",
      " [ 0.03849049]\n",
      " [-0.50866391]\n",
      " [-0.04236725]\n",
      " [ 0.63199991]\n",
      " [ 0.10357946]\n",
      " [-0.44478527]\n",
      " [ 0.72417282]\n",
      " [-0.14758787]\n",
      " [ 0.18837871]\n",
      " [ 0.58767086]\n",
      " [-0.45461451]\n",
      " [ 0.30508441]\n",
      " [ 0.48029641]\n",
      " [ 0.36601281]\n",
      " [ 0.33141664]\n",
      " [-0.4627632 ]\n",
      " [-0.30450461]\n",
      " [-0.06488085]\n",
      " [-0.16549144]\n",
      " [ 0.3292622 ]\n",
      " [ 0.10766861]\n",
      " [-0.78434496]\n",
      " [-0.1028879 ]\n",
      " [ 0.10951722]\n",
      " [-0.46481417]\n",
      " [ 0.73654007]\n",
      " [-0.12050342]\n",
      " [ 0.01149582]\n",
      " [-0.35985646]\n",
      " [ 0.486786  ]\n",
      " [ 0.45625972]\n",
      " [ 0.32966712]\n",
      " [-0.10367652]\n",
      " [-0.66659985]\n",
      " [ 0.05636904]\n",
      " [-0.16312702]\n",
      " [-0.15642711]\n",
      " [ 0.15889224]\n",
      " [ 0.1303199 ]\n",
      " [ 0.04521252]\n",
      " [ 0.30815538]\n",
      " [-0.08797288]\n",
      " [-0.09805195]\n",
      " [ 0.35670113]\n",
      " [-0.63721341]\n",
      " [-0.1397628 ]\n",
      " [ 0.20544726]\n",
      " [-0.11328957]\n",
      " [-0.60363562]\n",
      " [ 0.42070697]\n",
      " [-0.22969209]\n",
      " [-0.114072  ]\n",
      " [-0.18526522]\n",
      " [ 0.40864096]\n",
      " [-0.58117248]\n",
      " [ 0.16573685]\n",
      " [-0.21550746]\n",
      " [ 0.07714117]\n",
      " [ 0.05908837]\n",
      " [-0.23836393]\n",
      " [-0.03158275]\n",
      " [-0.48487714]\n",
      " [ 0.25855304]\n",
      " [ 0.09991667]\n",
      " [ 0.25808573]\n",
      " [-0.57150163]\n",
      " [ 0.04284057]\n",
      " [-0.28483402]\n",
      " [-0.20610842]\n",
      " [-0.14742537]\n",
      " [ 0.2208209 ]\n",
      " [-0.29615323]\n",
      " [-0.26888343]\n",
      " [ 0.6157229 ]\n",
      " [ 0.25265019]\n",
      " [-0.22312113]\n",
      " [ 0.52492467]\n",
      " [ 0.26281777]\n",
      " [ 0.21986458]\n",
      " [-0.42130046]\n",
      " [ 0.37441606]\n",
      " [ 0.15324329]\n",
      " [ 0.17502845]\n",
      " [-0.4165132 ]\n",
      " [ 0.02316595]\n",
      " [ 0.23165   ]\n",
      " [-0.85867419]\n",
      " [-0.152214  ]\n",
      " [-0.39778783]\n",
      " [-0.43757229]\n",
      " [ 0.41898895]\n",
      " [-0.00723776]\n",
      " [-0.46317384]\n",
      " [-0.2280338 ]\n",
      " [ 0.00632264]\n",
      " [-0.03857384]\n",
      " [ 0.15254261]\n",
      " [-0.02765897]\n",
      " [-0.37166895]\n",
      " [ 0.11782588]\n",
      " [ 0.48445925]\n",
      " [ 0.12550018]\n",
      " [-0.47235302]\n",
      " [-0.25344084]\n",
      " [ 0.36931165]\n",
      " [-0.0052482 ]\n",
      " [-0.3022102 ]\n",
      " [-0.17667736]\n",
      " [ 0.45483844]\n",
      " [ 0.09768438]\n",
      " [ 0.00334331]\n",
      " [ 0.02255512]\n",
      " [-0.02480216]\n",
      " [-0.22778205]\n",
      " [ 0.15712795]\n",
      " [ 0.19332581]\n",
      " [ 0.36183613]\n",
      " [ 0.35592435]\n",
      " [-0.52564411]\n",
      " [-0.17420512]\n",
      " [-0.43351724]\n",
      " [-0.23413792]\n",
      " [-0.18266653]\n",
      " [ 0.04399181]\n",
      " [ 0.34462292]\n",
      " [-0.1108868 ]\n",
      " [-0.31892977]\n",
      " [-0.09278599]\n",
      " [ 0.42804454]\n",
      " [ 0.48025987]\n",
      " [-0.32653396]\n",
      " [ 0.8565764 ]\n",
      " [-0.09832826]\n",
      " [-0.35210607]\n",
      " [-0.13652125]\n",
      " [-0.37383197]\n",
      " [-0.01469115]\n",
      " [ 0.01498466]\n",
      " [ 0.06362563]\n",
      " [-0.28767976]\n",
      " [-0.0414792 ]\n",
      " [-0.04770012]\n",
      " [ 0.16751246]\n",
      " [ 0.19848384]\n",
      " [-0.48670932]\n",
      " [ 0.38770001]\n",
      " [ 0.08688849]\n",
      " [-0.40500173]\n",
      " [-0.2990492 ]\n",
      " [ 0.56051172]\n",
      " [-0.39347745]\n",
      " [-0.69688021]\n",
      " [ 0.06228329]\n",
      " [ 0.14195747]\n",
      " [-0.53742014]\n",
      " [-0.16369491]\n",
      " [-0.58176194]\n",
      " [-0.51155498]\n",
      " [-0.07654442]\n",
      " [ 0.25190994]\n",
      " [-0.09494076]\n",
      " [ 0.87755872]\n",
      " [ 0.25248969]\n",
      " [-0.07876856]\n",
      " [ 0.56590403]\n",
      " [ 0.42739904]\n",
      " [ 0.45763904]\n",
      " [-0.04973006]\n",
      " [ 0.45942842]\n",
      " [-0.01539401]\n",
      " [ 0.11049564]\n",
      " [-0.51286026]\n",
      " [-0.08272913]\n",
      " [ 0.64172951]\n",
      " [-0.05242682]\n",
      " [-0.13544785]\n",
      " [ 0.07629606]\n",
      " [ 0.08630391]\n",
      " [ 0.16736881]\n",
      " [ 0.61028974]\n",
      " [ 0.0979066 ]\n",
      " [-0.14896397]\n",
      " [-0.85048068]\n",
      " [-0.2662785 ]\n",
      " [ 0.14813671]\n",
      " [ 0.27896922]\n",
      " [ 0.08197974]\n",
      " [ 0.32194448]\n",
      " [-0.14636657]\n",
      " [ 0.16864558]\n",
      " [-0.02673311]\n",
      " [ 0.15743262]\n",
      " [ 0.37590259]\n",
      " [-0.38742923]\n",
      " [-0.32882797]\n",
      " [ 0.0717381 ]\n",
      " [ 0.31617167]\n",
      " [-0.12270835]\n",
      " [ 0.00837684]\n",
      " [ 0.31588679]\n",
      " [ 0.3047372 ]\n",
      " [-0.25157671]\n",
      " [ 0.25216914]\n",
      " [-0.02072457]\n",
      " [-0.54582074]\n",
      " [-0.23816929]\n",
      " [ 0.16020994]\n",
      " [ 0.47923599]\n",
      " [-0.10851091]\n",
      " [ 0.28450724]\n",
      " [-0.0609874 ]\n",
      " [-0.708729  ]\n",
      " [ 0.03593663]\n",
      " [-0.23087819]\n",
      " [ 0.08540616]\n",
      " [ 0.28244244]\n",
      " [-0.16448862]\n",
      " [ 0.09339068]\n",
      " [ 0.29996579]\n",
      " [-0.15448283]\n",
      " [-0.28158002]\n",
      " [ 0.45625249]\n",
      " [ 0.02679633]\n",
      " [-0.0834188 ]\n",
      " [-0.12963363]\n",
      " [ 0.00547078]\n",
      " [-0.9665602 ]\n",
      " [ 0.16619561]\n",
      " [ 0.71219137]\n",
      " [ 0.05056636]\n",
      " [-0.01200752]\n",
      " [-0.32026907]\n",
      " [-0.43540387]\n",
      " [-0.44763987]\n",
      " [ 0.71429633]\n",
      " [-0.36816787]\n",
      " [-0.38409398]\n",
      " [-0.08954643]\n",
      " [ 0.10590403]\n",
      " [-0.42117649]\n",
      " [ 0.13274188]\n",
      " [ 0.11056091]\n",
      " [ 0.05973322]\n",
      " [-0.60281883]\n",
      " [ 0.05409619]\n",
      " [ 0.23573925]\n",
      " [ 0.63638831]\n",
      " [ 0.33155039]\n",
      " [ 0.03864522]\n",
      " [ 0.03017612]\n",
      " [-0.07400879]\n",
      " [ 0.3518965 ]\n",
      " [-0.42555952]\n",
      " [-0.13561327]\n",
      " [-0.08552091]\n",
      " [-0.13561185]\n",
      " [-0.02608275]\n",
      " [ 0.08698244]\n",
      " [ 0.08012217]\n",
      " [-0.21448606]\n",
      " [ 0.03241678]\n",
      " [-0.10358255]\n",
      " [-0.13495281]\n",
      " [-0.33959749]\n",
      " [ 0.52726625]\n",
      " [-0.2736512 ]\n",
      " [-0.0127851 ]\n",
      " [ 0.21672311]\n",
      " [-0.75764393]\n",
      " [-0.18052895]\n",
      " [-0.22016524]\n",
      " [-0.31930336]\n",
      " [-0.51122474]\n",
      " [-0.18182488]\n",
      " [ 0.38595838]\n",
      " [ 0.20771848]\n",
      " [-0.10130685]\n",
      " [ 0.09546352]\n",
      " [ 0.13257857]\n",
      " [ 0.07148276]\n",
      " [ 0.34039747]\n",
      " [-0.17939751]\n",
      " [-0.52747659]\n",
      " [-0.24582625]\n",
      " [-0.12878696]\n",
      " [-0.44967626]\n",
      " [ 0.33432195]\n",
      " [ 0.47319988]\n",
      " [ 0.02676899]\n",
      " [-0.59457743]\n",
      " [-0.05522989]\n",
      " [-0.0745871 ]\n",
      " [-0.1832365 ]\n",
      " [ 0.13968969]\n",
      " [-0.34224275]\n",
      " [-0.20168615]\n",
      " [-0.31282262]\n",
      " [ 0.24232577]\n",
      " [ 0.34803722]\n",
      " [-0.10749164]\n",
      " [ 0.3561988 ]\n",
      " [ 0.29092585]\n",
      " [ 0.08160331]\n",
      " [ 0.28814502]\n",
      " [ 0.13930815]\n",
      " [-0.56369639]\n",
      " [ 0.16163144]\n",
      " [ 0.25374972]\n",
      " [-0.12874646]\n",
      " [-0.31912466]\n",
      " [ 0.8552768 ]\n",
      " [ 0.50145636]\n",
      " [-0.55250565]\n",
      " [-0.5548992 ]\n",
      " [-0.08404405]\n",
      " [ 0.17101053]\n",
      " [-0.18284572]\n",
      " [ 0.40437966]\n",
      " [-0.41019612]\n",
      " [ 0.56719936]\n",
      " [ 0.11944851]\n",
      " [-0.2650742 ]\n",
      " [ 0.2768086 ]\n",
      " [-0.077992  ]\n",
      " [-0.34222627]\n",
      " [ 0.09384398]\n",
      " [ 0.70445099]\n",
      " [-0.19467936]\n",
      " [-0.8440531 ]\n",
      " [-0.36696986]\n",
      " [-0.21478907]\n",
      " [ 0.25351179]\n",
      " [ 0.4205791 ]\n",
      " [ 0.44035763]\n",
      " [ 0.14891078]\n",
      " [-0.09114535]\n",
      " [-0.01788888]\n",
      " [-0.39780744]\n",
      " [-0.449516  ]\n",
      " [ 0.92505408]\n",
      " [ 0.59187466]\n",
      " [-0.80468124]\n",
      " [-0.31384769]\n",
      " [-0.13578263]\n",
      " [ 0.91543597]\n",
      " [ 0.57873392]\n",
      " [ 0.28551678]\n",
      " [-0.35435113]\n",
      " [ 0.44795035]\n",
      " [-0.20899892]\n",
      " [-0.12110691]\n",
      " [ 0.28387931]\n",
      " [ 0.72959701]\n",
      " [-0.21187778]\n",
      " [ 0.52102251]\n",
      " [ 0.00723962]\n",
      " [-0.11496016]\n",
      " [-0.10660565]\n",
      " [ 0.27306639]\n",
      " [-0.35876751]\n",
      " [ 0.0747293 ]\n",
      " [ 0.04675994]\n",
      " [ 0.06355624]\n",
      " [ 0.00671438]\n",
      " [-0.63655252]\n",
      " [ 0.16089731]\n",
      " [ 0.11710634]\n",
      " [ 0.20360693]\n",
      " [ 0.47847076]\n",
      " [ 0.33428657]\n",
      " [-0.78655168]\n",
      " [-0.48742174]\n",
      " [-0.45762754]\n",
      " [-0.20654348]\n",
      " [ 0.0104507 ]\n",
      " [-0.10492789]\n",
      " [ 0.19213681]\n",
      " [-0.32111996]\n",
      " [-0.50247701]\n",
      " [-0.22604935]\n",
      " [-0.06882735]\n",
      " [ 0.36055088]\n",
      " [-0.3197489 ]\n",
      " [ 0.80918181]\n",
      " [-0.25119396]\n",
      " [-0.12931479]\n",
      " [ 0.15875354]\n",
      " [ 0.44375055]\n",
      " [-0.3064686 ]\n",
      " [-0.22283822]\n",
      " [ 0.41766006]\n",
      " [ 0.33223171]\n",
      " [ 0.1496959 ]\n",
      " [-0.43548087]\n",
      " [-0.66227456]\n",
      " [ 0.24941536]\n",
      " [-0.27685252]\n",
      " [-0.25802971]\n",
      " [-0.23902869]\n",
      " [-0.31574895]\n",
      " [-0.21793583]\n",
      " [-0.26659564]\n",
      " [ 0.21580765]\n",
      " [-0.27745436]\n",
      " [-0.02481297]\n",
      " [-0.25471165]\n",
      " [ 0.24267635]\n",
      " [ 0.22868607]\n",
      " [-0.32665976]\n",
      " [ 0.22728336]\n",
      " [ 0.15024827]\n",
      " [-0.09030833]\n",
      " [ 0.60833079]]\n"
     ]
    }
   ],
   "source": [
    "print(theta)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "1c8120b9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1a374a289c8>]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAawUlEQVR4nO3dfXBcV5nn8e/T3Xq13mxJVmRZRjYxCQ4BJ6u84mFCQiCE2XXYYhhnpogHsmVqJ2xBMVW7yUztLNRWtsLOQHZSy0LMJhC2GCA7M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jY7vBP/0dB22bHMEP0JYJu7fyPrUJT7XB9G7JhZGZn3GLaRCflPhdNG9nn43+JTwK88M/m6BlgRVpYsBBYD62ekExPk7g+5+3x3byHz/+iv3P1PiHCfzWyWmVUOb5P5mdzCTP9s5/uNhym+aXE3mVUWO4G/zHc9U+zLD4EDwACZubX7ycw1rgN2AL8E5oRzDfhm6PcbQGvW83wOaA+3z+a7X5fo7zIy85KbgY3hdnfE+/x+4Lehz1uAvwrti8gEVTvwf4GS0F4a9tvD8UVZz/WX4d9iO/DxfPdtnP2/jXOrZSLb59C3TeG2dTibZvpnW5cfEBGJoEKelhERkYtQuIuIRJDCXUQkghTuIiIRpHAXEYkghbuISAQp3EVEIuj/Axvd6pTEYAhbAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "print(J_hist.size)\n",
    "x_axis = np.linspace(0,5000,5000)\n",
    "plt.plot(x_axis,J_hist[0:5000])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb167fa0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "9831fa2b",
   "metadata": {},
   "source": [
    "for line in locals()['In']:\n",
    "    print(line)\n",
    "    \n",
    "后面的都是草稿, 还有一定价值,没删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a02625d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#function [X,err,iter] = Gauss_S(A,X0,b,n,step,tol)\n",
    "            #迭代法求解好像必须是n*n型的正定?\n",
    "def jacobi(X,Y,tol,step):\n",
    "    n = X.shape[1] # 获取维度 python操作\n",
    "    m = X.shape[0]\n",
    "    print('m row =',m)\n",
    "    print('n col =',n)\n",
    "    theta0 = np.zeros(n)# 创建0向量\n",
    "    theta  = np.zeros(n)\n",
    "    print('step =',step)\n",
    "    for i in range(int(step)):\n",
    "        err = 0\n",
    "#         print('i=',i)\n",
    "        for j in range(m):\n",
    "            theta[j] = (Y[j]-X[j].dot(theta0)+X[j,j]*theta0[j])/X[j,j]\n",
    "        theta0 = theta\n",
    "        \n",
    "        err = np.linalg.norm(X.dot(theta)-Y)\n",
    "        if(err < tol):\n",
    "            print(\"迭代训练结束,迭代次数:\"+i+\", 偏差值:\"+err)\n",
    "    return (theta,err)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e40564e4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "\n",
    "(theta,err) = jacobi(A,B,tol,step)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6f89bc2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f9ae286",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(A[1].size)\n",
    "print(A[1,1].size)\n",
    "print(A[1,:].size)\n",
    "print(X0[1].size)\n",
    "print(X0.size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acfdb2a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "    print(A.dot(X0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42929e18",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 草稿部分 \n",
    "#def jacobi(A,B,sigma,N):\n",
    "#     n = len(A)\n",
    "#     x0 = []\n",
    "#     x = []\n",
    "#     for i in range(0,n):\n",
    "#         x0.append(0)\n",
    "#         x.append(0)\n",
    "#     for k in range(1,N+1):\n",
    "#         R = 0\n",
    "#         for i in range(0,n):\n",
    "#             sum_ax = 0\n",
    "#             for j in range(0,n):\n",
    "#                 sum_ax = sum_ax + A[i][j] * x0[j]\n",
    "#             x[i] = x0[i] + ((B[i] - sum_ax)/A[i][i])\n",
    "#             if abs(x[i] - x0[i]) > R:\n",
    "#                 R = abs(x[i] - x0[i])\n",
    "#         if R <= sigma:\n",
    "#             print(\"精确度等于\",sigma,\"时，jacobi迭代法需要迭代\",k,\"次收敛\")\n",
    "#             return (x,k)\n",
    "#         for i in range(0,n):\n",
    "#             x0[i] = x[i]\n",
    "#     return (x,k)\n",
    "# def Jacobi(A,X0,B,n,step,tol):\n",
    "#     theta = X0\n",
    "#     for i in range(step):\n",
    "#         error=0\n",
    "#         for j in range(n):\n",
    "#             theta[j] = (B[j]-A[j,:]*X0+A[j,j]*X0[j])/A[j,j]\n",
    "# #             print(theta[j])\n",
    "# #             print(A[j,j])\n",
    "#         X0 = theta\n",
    "#         err = np.linalg.norm(A*theta-B-0)\n",
    "#         if(err < tol):\n",
    "#             print(\"迭代次数:\"+i+\", 偏差值:\"+err)\n",
    "# #AX = b Xθ=Y\n",
    "# # def = jacobi(A,B,tol,step):\n",
    "# #     n = A.ndim\n",
    "# #     theta0 = np.zeros(n)\n",
    "# #     theta  = np.zeros(n)\n",
    "# #     for i in range(step):\n",
    "# #         err = 0\n",
    "# #         print('i=',i)\n",
    "# #         for j in range(n):\n",
    "# #             theta[j] = (B[j]-A[j].dot(theta0)+A[j,j]*theta0[j])/A[j,j]\n",
    "\n",
    "# #         err = np.linalg.norm(A*theta-B-0)\n",
    "\n",
    "        \n",
    "            \n",
    "        \n",
    "# def jacobi(a,b,c=0.0001,d=30):\n",
    "#     x1=np.zeros(a.shape[1])\n",
    "#     x2=np.zeros(a.shape[1])\n",
    "#     k=0\n",
    "#     while k<d:\n",
    "#         k=k+1\n",
    "#         print('k=',k)\n",
    "#         for i in range(a.shape[1]):\n",
    "#             x2[i]=(-a[i].dot(x1)+b[i]+a[i,i]*x1[i])/a[i,i]\n",
    "#         if np.max(np.abs(x2-x1))<=c:\n",
    "#             print(\"x%d=\" % k,x2)\n",
    "#             print(np.max(np.abs(x2-x1)))\n",
    "#             break\n",
    "#         print(\"x%d=\" % k,x2)\n",
    "#         x1=x2.copy()\n",
    "#     return x2\n",
    "\n",
    "# def gauss_seidel(A,B,sigma,N):\n",
    "#     n = len(A)\n",
    "#     x0 = []\n",
    "#     x = []\n",
    "#     for i in range(0,n):\n",
    "#         x0.append(0)\n",
    "#         x.append(0)\n",
    "#     for k in range(1,N+1):\n",
    "#         R = 0\n",
    "#         for i in range(0,n):\n",
    "#             sum_ax = 0\n",
    "#             for j in range(0,n):\n",
    "#                 if j >= i:\n",
    "#                     sum_ax = sum_ax + A[i][j] * x0[j]\n",
    "#                 else:\n",
    "#                     sum_ax = sum_ax + A[i][j] * x[j]\n",
    "#             x[i] = x0[i] + ((B[i] - sum_ax)/A[i][i])\n",
    "#             if abs(x[i] - x0[i]) > R:\n",
    "#                 R = abs(x[i] - x0[i])\n",
    "#         if R <= sigma:\n",
    "#             print(\"精确度等于\",sigma,\"时，gauss_seidel迭代法需要迭代\",k,\"次收敛\")\n",
    "#             return (x,k)\n",
    "#         for i in range(0,n):\n",
    "#             x0[i] = x[i]\n",
    "#     return (x,k)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
